0

Would like to ask some advice when working with xml data with C#. I have a small practice exercise where I am required to retrieve a specific text value at a specific tag.

I have assigned the various names of the element nodes to string values and the the user is required to input a string value to the console and if the name tag is the same as the input then to retrieve the text value positioned at that tag. This is the C# code I used but I am not sure how to retrieve the text value at the name tag.

int priceSpecific;
        string destination;
        ArrayList array = new ArrayList();
        xRootNode = xdoc.DocumentElement;

        string firstValue = xRootNode.FirstChild.FirstChild.Name;
        string secondValue = xRootNode.FirstChild.FirstChild.NextSibling.Name;
        string thirdValue = xRootNode.FirstChild.FirstChild.NextSibling.NextSibling.Name;
        string fourthValue = xRootNode.FirstChild.FirstChild.NextSibling.NextSibling.NextSibling.Name;
        array.AddRange(new object[] { firstValue, secondValue, thirdValue, fourthValue});

        Console.WriteLine("Please enter your destination, first letter capital");
        destination = Console.ReadLine();

The idea is to loop through the arraylist and retrieve the name of the element node that is the same as the string input of the user. Any advice as to how to retrieve the text value?

Regards

1
  • Are you trying to select the node by name and value of it? Commented May 25, 2011 at 7:59

2 Answers 2

1

That is some pretty nasty looking code there! I would recommend that you spend a few hours learning about Linq-to-XML. roughly speaking, if you want to find the value of an element with a given name, it can be done as follows:

string elementName = "foo";
XDocument doc = XDocument.Parse("<xml document goes here>");
string matchedValue = doc.Descendants(elementName).Single().Value;

Much simpler!

Sign up to request clarification or add additional context in comments.

Comments

1

You can use several approaches, most usable in your scenario seem to be:

  1. XmlDocument + XPath (supported in all .NET versions)
  2. XmlReader (supported in all .NET versions)
  3. XDocument (supported with LINQ since .NET 3.0)
  4. XDocument with LINQ syntax

Choices 3 or 4 are preferred if .NET 3 or above is available and xml document is not too big (document size of several MB is the boundary).

Choice 1 uses XPath, which allows very strong queries into the document structure


1.

XPathDocument document = new XPathDocument(@"myFile.xml");
XPathNavigator navigator = document.CreateNavigator();
string foundElementContent = 
  navigator.SelectSingleNode("//myElement[position()=1]/text()").ToString();

2.

string elementNameToFind = "myElement";
XmlReader xmlReader = XmlReader.Create(@"myFile.xml");
string foundElementContent = string.Empty;
while (xmlReader.Read())
{
   if(xmlReader.NodeType==XmlNodeType.Element &&
      xmlReader.Name == elementNameToFind)
   {
     foundElementContent=xmlReader.ReadInnerXml();
     break;
   }
}
xmlReader.Close();

3.

string elementNameToFind = "myElement";
XDocument xmlInMemoryDoc = XDocument.Load(@"myFile.xml");
XElement foundElement = xmlInMemoryDoc.Descendants(elementNameToFind).First();

4.

string elementNameToFind = "myElement";
XDocument xmlInMemoryDoc = XDocument.Load(@"myFile.xml");
XElement foundElement =
  (
     from e in xmlInMemoryDoc.Descendants()
     where e.Name == elementNameToFind
     select e
  ).First();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.