XElement-读取节点C#
我有一个如下所示的XML:XElement-读取节点C#,c#,xml,linq,xelement,C#,Xml,Linq,Xelement,我有一个如下所示的XML: <?xml version="1.0"?> <productfeed> <product> <name>x</name> <etc>z</etc> </product> <product> <name>x</name> <etc>z<
<?xml version="1.0"?>
<productfeed>
<product>
<name>x</name>
<etc>z</etc>
</product>
<product>
<name>x</name>
<etc>z</etc>
</product>
</productfeed>
正式地,您应该将其加载到XDocument中。它将有一个
元素作为根属性
XElement允许您跳过此步骤,它可以同时充当文档和根。解决方法非常简单:
XElement productFeed = root; //.Element("productfeed");
XElement
和XDocument
确实解析相同的内容,但是XDocument有一个(更完整的)文档包装器,在处理时需要检查XElement.document属性。我想这就是你要找的。root
是
。你获得“纯内容”的意思是什么?前面的评论给出的答案很有用。但是,如果我尝试:XElement root=XElement(“…path…”);或XDocument rootDocument=XDocument(“…路径…”);解析的结果是完全相同的。那么会有什么不同呢?好的,也许它们被解析为相同的,但是我看到有一个不同。加载时使用XDocument时,我可以一步一步地进行。谢谢
XElement productFeed = root; //.Element("productfeed");