如何使用c#在XML文件中获取节点值

本文关键字:获取 节点 文件 XML 何使用 | 更新日期: 2023-09-27 17:53:36

我有一个XML文件,在这个XML中可以看到RESPONSE_DATA标记。这个标签有更多的内部标签。我需要得到所有的值在PERSON_DATA标签。我还需要得到所有的其他值在下面的xml文件。

<?xml version='"1.0'" encoding='"utf-16'"?>'r'n
<HUMAN_VERIFICATION xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
                    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <RESPONSE_DATA>
      <RESPONSE_STATUS>
         <ERROR>100</ERROR>
         <MESSAGE>successful</MESSAGE>
      </RESPONSE_STATUS>
      <CONTACT_NUMBER>3120202456011</CONTACT_NUMBER>
      <PERSON_DATA>
         <NAME>Alex</NAME>
         <DATE_OF_BIRTH>10-9-1982</DATE_OF_BIRTH>
         <BIRTH_PLACE>Washington</BIRTH_PLACE>
         <EXPIRY>2020-12-15</EXPIRY>
      </PERSON_DATA>
      <CARD_TYPE>idcard</CARD_TYPE>
   </RESPONSE_DATA>
</HUMAN_VERIFICATION>

如何使用c#在XML文件中获取节点值

我更喜欢使用Linq而不是Xml

var results = doc.Descendants("PERSON_DATA")      // Flatten the hierarchy and look for PERSON_DATA                
    .Select(x=> new 
            {
                NAME = (string)x.Element("NAME"),
                DATE_OF_BIRTH = (string)x.Element("DATE_OF_BIRTH"),
                BIRTH_PLACE = (string)x.Element("BIRTH_PLACE"),
                EXPIRY = (string)x.Element("EXPIRY"),
            });

检查Demo

您可以尝试一下这段代码,它可能对您有帮助。

XmlDocument newdoc = new XmlDocument();
newdoc.InnerXml = " <?xml version='"1.0'" encoding='"utf-16'"?><HUMAN_VERIFICATION><RESPONSE_DATA><RESPONSE_STATUS><ERR>100</ERROR><MESSAGE>successful</MESSAGE></RESPONSE_STATUS><CONTACT_NUMBER>3120202456011</ CONTACT _NUMBER><PERSON_DATA><NAME>Alex</NAME><DATE_OF_BIRTH>10-9-1982</DATE_OF_BIRTH><BIRTH_PLACE>Washington</BIRTH_PLACE><EXPIRY>2020-12-15</EXPIRY></PERSON_DATA><CARD_TYPE>idcard</CARD_TYPE></RESPONSE_DATA></HUMAN_VERIFICATION>";
var selectnode = "HUMAN_VERIFICATION/RESPONSE_DATA/PERSON_DATA";
var nodes = newdoc.SelectNodes(selectnode);
foreach (XmlNode nod in nodes)
{
    string name   = nod["NAME"         ].InnerText;
    string dob    = nod["DATE_OF_BIRTH"].InnerText;
    string place  = nod["BIRTH_PLACE"  ].InnerText;
    string expiry = nod["EXPIRY"       ].InnerText;
    Console.WriteLine("Person: {0} {1} {2} {3}", name, dob, place, expiry);
}

使用System.Xml.Linq非常简单直观。

var xml = "<?xml version='"1.0'" encoding='"utf-16'"?>'r'n<HUMAN_VERIFICATION xmlns:xsi='"http://www.w3.org/2001/XMLSchema-instance'" xmlns:xsd='"http://www.w3.org/2001/XMLSchema'">'r'n  <RESPONSE_DATA>'r'n    <RESPONSE_STATUS>'r'n      <ERROR>100</ERROR>'r'n      <MESSAGE>successful</MESSAGE>'r'n    </RESPONSE_STATUS>'r'n    <CONTACT_NUMBER>3120202456011</CONTACT_NUMBER>'r'n    <PERSON_DATA>'r'n      <NAME>Alex</NAME>'r'n     <DATE_OF_BIRTH>10-9-1982</DATE_OF_BIRTH>'r'n      <BIRTH_PLACE>Washington</BIRTH_PLACE>'r'n      <EXPIRY>2020-12-15</EXPIRY>'r'n    </PERSON_DATA>'r'n    <CARD_TYPE>idcard</CARD_TYPE>'r'n  </RESPONSE_DATA>'r'n</HUMAN_VERIFICATION>";
var document = XDocument.Parse(xml);
var name = document.Element("HUMAN_VERIFICATION").Element("RESPONSE_DATA").Element("PERSON_DATA").Element("NAME").Value;

var personData = document.Element("HUMAN_VERIFICATION").Element("RESPONSE_DATA").Element("PERSON_DATA").Elements().ToDictionary(e => e.Name.ToString(), e => e.Value);