C# 如何从 XDocument 获取 XML 节点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/752271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 23:48:22  来源:igfitidea点击:

How to Get XML Node from XDocument

c#.netlinqlinq-to-xml

提问by

How to Get an XML Element from XDocument using LINQ ?

如何使用 LINQ 从 XDocument 获取 XML 元素?

Suppose I have an XDocument Named XMLDoc which is shown below:

假设我有一个名为 XMLDoc 的 XDocument,如下所示:

<Contacts>
       <Node>
           <ID>123</ID>
           <Name>ABC</Name>
       </Node>
       <Node>
           <ID>124</ID>
           <Name>DEF</Name>
       </Node>
</Contacts>

XElement Contacts = from xml2 in XMLDoc.Elements("Contacts").Elements("Node")
                    where xml2.Element("ID").Value == variable
                    select xml2;

But I am getting Error "Object Reference is NOT to set....."

但是我收到错误“对象引用不是要设置.....”

How to get a particular Node from a XML file using LINQ ? And I want to update some values in that node ?

如何使用 LINQ 从 XML 文件中获取特定节点?我想更新该节点中的一些值?

How it is possible ????

怎么可能???

Thanks in advance.........

提前致谢.........

回答by marc_s

The .Elements operation returns a LIST of XElements - but what you really want is a SINGLE element. Add this:

.Elements 操作返回 XElements 的列表 - 但您真正想要的是一个 SINGLE 元素。添加这个:

XElement Contacts = (from xml2 in XMLDoc.Elements("Contacts").Elements("Node")
                    where xml2.Element("ID").Value == variable
                    select xml2).FirstOrDefault();

This way, you tell LINQ to give you the first (or NULL, if none are there) from that LIST of XElements you're selecting.

通过这种方式,您可以告诉 LINQ 从您选择的 XElements 列表中给您第一个(或 NULL,如果没有)。

Marc

马克

回答by Ondrej Slinták

test.xml:

测试.xml:

<?xml version="1.0" encoding="utf-8"?>
<Contacts>
  <Node>
    <ID>123</ID>
    <Name>ABC</Name>
  </Node>
  <Node>
    <ID>124</ID>
    <Name>DEF</Name>
  </Node>
</Contacts>

Select a single node:

选择单个节点:

XDocument XMLDoc = XDocument.Load("test.xml");
string id = "123"; // id to be selected

XElement Contact = (from xml2 in XMLDoc.Descendants("Node")
                    where xml2.Element("ID").Value == id
                    select xml2).FirstOrDefault();

Console.WriteLine(Contact.ToString());

Delete a single node:

删除单个节点:

XDocument XMLDoc = XDocument.Load("test.xml");
string id = "123";

var Contact = (from xml2 in XMLDoc.Descendants("Node")
               where xml2.Element("ID").Value == id
               select xml2).FirstOrDefault();

Contact.Remove();
XMLDoc.Save("test.xml");

Add new node:

添加新节点:

XDocument XMLDoc = XDocument.Load("test.xml");

XElement newNode = new XElement("Node",
    new XElement("ID", "500"),
    new XElement("Name", "Whatever")
);

XMLDoc.Element("Contacts").Add(newNode);
XMLDoc.Save("test.xml");