开发者

retrieving xml text value

开发者 https://www.devze.com 2023-03-08 09:51 出处:网络
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.

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.Ne开发者_如何学CxtSibling.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


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!


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();
0

精彩评论

暂无评论...
验证码 换一张
取 消