C#XmlDocument SelectNodes无法正常工作 [英] C# XmlDocument SelectNodes is not working

查看:50
本文介绍了C#XmlDocument SelectNodes无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从XML文件中获取值,但是失败了.您能帮我指出问题吗?因为我已经非常努力地进行测试和谷歌搜索,但是仍然无法发现问题.

I want to get the value from XML file but I failed. Can you please help me point out the problem ?? Because I already tried very hard to test and googling but I still can't spot the problem.

XML:

<?xml version="1.0" encoding="utf-8" ?> 
<Contacts>
  - <Contact>
    <ID>xxx</ID> 
      <AutoUpdateEnabled>false</AutoUpdateEnabled> 
      <LastChanged>2013-05-29T01:53:59.4470000Z</LastChanged> 
    - <Profiles>
        - <Personal>
              <FirstName>My First Name</FirstName> 
              <LastName>My Last Name</LastName> 
              <UniqueName>My Unique Name</UniqueName> 
              <SortName></SortName> 
              <DisplayName>My Display Name</DisplayName> 
          </Personal>
    </Profiles>
    - <Phones>
        - <Phone>
          <ID>3</ID> 
          <PhoneType>Mobile</PhoneType> 
          <Number>000-0000000</Number> 
          <IsIMEnabled>false</IsIMEnabled> 
          <IsDefault>false</IsDefault> 
          </Phone>
    </Phones>
    - <Locations>
        - <Location>
              <ID>2</ID> 
              <LocationType>Business</LocationType> 
              <CompanyName></CompanyName> 
              <IsDefault>false</IsDefault> 
          </Location>
      </Locations>
</Contact>
- <Contact>
  <ID>xxx</ID> 
  <AutoUpdateEnabled>false</AutoUpdateEnabled> 
  <LastChanged>2013-05-29T01:53:25.2670000Z</LastChanged> 
    - <Profiles>
        - <Personal>
              <FirstName>Person</FirstName> 
              <LastName>Two</LastName> 
              <UniqueName></UniqueName> 
              <SortName></SortName> 
              <DisplayName>Person Two</DisplayName> 
          </Personal>
      </Profiles>
    - <Emails>
        - <Email>
              <ID>1</ID> 
              <EmailType>Personal</EmailType> 
              <Address>MyTest@gmail.com</Address> 
              <IsIMEnabled>false</IsIMEnabled> 
              <IsDefault>true</IsDefault> 
          </Email>
      </Emails>
    - <Locations>
        - <Location>
              <ID>2</ID> 
              <LocationType>Business</LocationType> 
              <CompanyName>Testing Company</CompanyName> 
              <IsDefault>false</IsDefault> 
          </Location>
      </Locations>
    </Contact>
 </Contacts>

我的示例代码:

XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml("TheXMLFile.xml");

xmldoc.DocumentElement.SelectNodes("contact")  // return 0 counts
xmldoc.DocumentElement.SelectNodes("/contact")  // return 0 counts
xmldoc.DocumentElement.SelectNodes("/contact")  // return 0 counts
xmldoc.DocumentElement.SelectNodes("/contacts/contact")  // return 0 counts
xmldoc.DocumentElement.SelectNodes("*")  // return 2 counts  !this works

XmlNodeList elemList = xmldoc.DocumentElement.GetElementsByTagName("contact"); // return 2 counts  !this also works
foreach (XmlNode node in elemList)
{    
    node.SelectSingleNode("Profiles")  //return ""
    node.SelectSingleNode("/Profiles")  //return ""
    node.SelectSingleNode("//Profiles")  //return ""
    node.SelectSingleNode(".//Profiles")  //return ""
}

我只想获取名字,姓氏,电子邮件地址", SelectNodes 函数无法正常工作...根本不知道...请帮助.预先感谢

I just want to get "FirstName, LastName, Email Address", the SelectNodes function just not working like expected... No clue at all ... please help. Thanks in advance

推荐答案

您需要这样的内容:

XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(@"D:\temp\contacts.xml");  // use the .Load() method - not .LoadXml() !!

// get a list of all <Contact> nodes
XmlNodeList listOfContacts = xmldoc.SelectNodes("/Contacts/Contact");

// iterate over the <Contact> nodes
foreach (XmlNode singleContact in listOfContacts)
{
   // get the Profiles/Personal subnode
   XmlNode personalNode = singleContact.SelectSingleNode("Profiles/Personal");

   // get the values from the <Personal> node
   if (personalNode != null)
   {
      string firstName = personalNode.SelectSingleNode("FirstName").InnerText;
      string lastName = personalNode.SelectSingleNode("LastName").InnerText;
   }

   // get the <Email> nodes
   XmlNodeList emailNodes = singleContact.SelectNodes("Emails/Email");

   foreach (XmlNode emailNode in emailNodes)
   {
      string emailTyp = emailNode.SelectSingleNode("EmailType").InnerText;
      string emailAddress = emailNode.SelectSingleNode("Address").InnerText;
   }
}

通过这种方法,您应该能够正确读取所需的所有数据.

With this approach, you should be able to read all the data you need properly.

这篇关于C#XmlDocument SelectNodes无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆