本文介绍了读取xml根节点参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨frnds ..,



我有一个任务,根节点参数名称应绑定在列表框中

(i 。,e)



我的xml文件



Hi frnds..,

I have an task that the Root node parameter name should bind in an list box
(i.,e)

My xml File

<?xml version="1.0" encoding="UTF-8"?>
-<catalog> -<book id="bk101"> <author>Gambardella, Matthew</author> <title>XML Developer's Guide</title> <genre>Computer</genre> <price>44.95</price> <publish_date>2000-10-01</publish_date> <description> An in-depth look at creating applications with XML. </description> </book> -<book id="bk102"> <author>Ralls, Kim</author> <title>Midnight Rain</title> <genre>Fantasy</genre> <price>5.95</price> <publish_date>2000-12-16</publish_date> <description> A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world. </description> </book>





我在列表框中的输出应该是



My Output in listbox should be as

book id
author
title
genre
price
publish_date
description







如果有可能请给我一些可能的解决方案感谢你好




Is it possible if so pls give me some possible solutions Thank YOu

推荐答案

var doc = new XDocument();
doc.Load(something);
List<string> list=new List<string>();
foreach(var name in doc.Root.Element("catalog").DescendantNodes().OfType<XElement>()
        .Select(x => x.Name).Distinct())
{
    list.Add(name);
}

//Bind the list to Listbox





希望这有帮助



Hope this helps


XDocument doc = XDocument.Load("Book.xml"); if (doc != null) { foreach (var name in doc.Root.DescendantNodes().OfType().Select(x => x.Name).Distinct()) { ListBox1.Items.Add(name.ToString()); } }





它给出了精确的结果....



It gives Exact result....


这篇关于读取xml根节点参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 22:07