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>[email protected]</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>
My Sample Code :
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 ""
}
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
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.
What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.
C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.
You need something like this:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With