简体   繁体   English

检索 xml 文本值

[英]retrieving xml text value

Would like to ask some advice when working with xml data with C#.想在使用 C# 处理 xml 数据时提出一些建议。 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.这是我使用的 C# 代码,但我不确定如何检索名称标签上的文本值。

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.思路是遍历arraylist,取回与用户输入的字符串相同的元素节点名称。 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 .我建议您花几个小时学习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) XmlDocument + XPath (支持所有 .NET 版本)
  2. XmlReader (supported in all .NET versions) XmlReader (支持所有 .NET 版本)
  3. XDocument (supported with LINQ since .NET 3.0) XDocument (自 .NET 3.0 起支持 LINQ)
  4. XDocument with LINQ syntax具有 LINQ 语法的 XDocument

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).如果 .NET 3 或以上可用且 xml 文档不太大(以几 MB 的文档大小为边界),则首选选项 3 或 4

Choice 1 uses XPath, which allows very strong queries into the document structure选择 1 使用 XPath,它允许对文档结构进行非常强的查询


1. 1.

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

2. 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. 3.

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

4. 4.

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM