C# XDocument 从 XML 文件中读取所有节点

用户6334610

我有一个这样的 XML 文件:

<Inventory>
<Item>
<Name>Super Mario Bros</Name>
<Count>14</Count>
<Price>29,99</Price>
<Comment><No Comments on this Product></Comment>
<Artist>N/A</Artist>
<Publisher>Nintendo</Publisher>
<Genre>Video Games</Genre>
<Year>1985</Year>
<ProductID>001</ProductID>
</Item>
<Item>
<Name>The Legend of Zelda</Name>
<Count>12</Count>
<Price>34,99</Price>
<Comment><No Comments on this Product></Comment>
<Artist>N/A</Artist>
<Publisher>Nintendo</Publisher>
<Genre>Video Games</Genre>
<Year>1986</Year>
<ProductID>002</ProductID>
</Item>
<Item>
<Name>Street Fighter</Name>
<Count>82</Count>
<Price>19,99</Price>
<Comment><No Comments on this Product></Comment>
<Artist>N/A</Artist>
<Publisher>Nintendo</Publisher>
<Genre>Video Games</Genre>
<Year>1987</Year>
<ProductID>003</ProductID>
</Item>
</Inventory>

(有更多的 Items,但它们都是一样的,除了值。)

现在我想遍历每个 Item 并从每个节点中提取每个值。这是我迄今为止尝试过的:

        var xDocument = XDocument.Load(FilePath_CSVToXML);
        string xml = xDocument.ToString();
        StringBuilder sb = new StringBuilder();

        foreach (XElement xe in xDocument.Descendants("Inventory")) {
            sb.Append(xe);
        }

        Console.WriteLine(sb.ToString());
        Console.ReadLine();

上面的代码正确地显示了 XML 文件,但它保留了 Nodes.xml 文件。(名称、数量、价格等)我只想要这些值。

巴厘岛 C

您需要使用该Value属性,即sb.Append(xe.Value).

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

c#使用XDocument读取XML注释

来自分类Dev

C#XDocument XML格式

来自分类Dev

在C#中将2个Xml文件与XDocument相交

来自分类Dev

Xml文件不适用于所有使用C#中的XDocument.Load的事件方法

来自分类Dev

C#XDocument从XML获取所有图像,并在LongListSelector中显示它们

来自分类Dev

XDocument加载xml文件问题

来自分类Dev

xml XDocument保存修改文件

来自分类Dev

C#XML复杂查询到XDocument

来自分类Dev

使用C#和XML(XDocument)的“:”错误

来自分类Dev

C#XML复杂查询到XDocument

来自分类Dev

插入/替换xml到Xdocument的指定节点

来自分类Dev

c#中的窄xdocument

来自分类Dev

使用XDocument在C#中将元素添加到xml文件

来自分类Dev

字符串中的XDocument XML模式

来自分类Dev

C#XML XDocument解析)如何知道具有特定孙代值的特定祖先节点的名称?

来自分类Dev

C#XDocument-将值添加到现有xml

来自分类Dev

使用XDocument和XElement如何在C#中向XML添加XAttribute和值

来自分类Dev

如何使用XDocument从c#中的复杂类型xml解析文本?

来自分类Dev

如何在根文件夹中使用XDocument保存在C#中创建的XML文件

来自分类Dev

根据现有的XML文件创建XDocument

来自分类Dev

C#XML避免使用XDocument进行html编码

来自分类Dev

XDocument保存后XML文件中的多余字符

来自分类Dev

XDocument保存后XML文件中的多余字符

来自分类Dev

XDocument写重复的xml

来自分类Dev

Linq to Xml:XDocument是缓存读取器吗?

来自分类Dev

使用XDocument读取XML的一部分

来自分类Dev

使用XDocument读取XML的一部分

来自分类Dev

在具有多个相似节点的C#中读取XML

来自分类Dev

使用XDocument使用MinOccurance从文件对XSD进行XML

Related 相关文章

热门标签

归档