gpt4 book ai didi

C# XmlDocument SelectNodes 不工作

转载 作者:可可西里 更新时间:2023-11-01 09:07:25 24 4
gpt4 key购买 nike

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

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 函数没有按预期工作...完全没有线索...请帮忙。提前致谢

最佳答案

你需要这样的东西:

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;
}
}

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

关于C# XmlDocument SelectNodes 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16889895/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com