Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 检索xml文本值_C#_Xml - Fatal编程技术网

C# 检索xml文本值

C# 检索xml文本值,c#,xml,C#,Xml,希望在使用C#处理xml数据时提出一些建议。 我有一个小练习,要求我检索特定标记处的特定文本值 我已将元素节点的各种名称分配给字符串值,用户需要向控制台输入字符串值,如果名称标记与输入相同,则检索位于该标记处的文本值。 这是我使用的C#代码,但我不确定如何检索name标记处的文本值 int priceSpecific; string destination; ArrayList array = new ArrayList(); xRootNode

希望在使用C#处理xml数据时提出一些建议。 我有一个小练习,要求我检索特定标记处的特定文本值

我已将元素节点的各种名称分配给字符串值,用户需要向控制台输入字符串值,如果名称标记与输入相同,则检索位于该标记处的文本值。 这是我使用的C#代码,但我不确定如何检索name标记处的文本值

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();
其思想是循环遍历arraylist并检索元素节点的名称,该名称与用户的字符串输入相同。 关于如何检索文本值有什么建议吗


关于

这是一些看起来很糟糕的代码!我建议你花几个小时来学习。粗略地说,如果要查找具有给定名称的元素的值,可以按如下方式进行:

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

简单多了

您可以使用几种方法,在您的场景中最有用的方法似乎是:

  • XmlDocument+XPath(所有.NET版本都支持)
  • XmlReader(所有.NET版本都支持)
  • XDocument(自.NET 3.0以来,LINQ支持)
  • xLINQ语法文档
  • 如果.NET 3或更高版本可用,并且xml文档不太大(文档大小以几MB为边界),则首选选项3或4

    选项1使用XPath,它允许对文档结构进行非常强的查询


    1.

    XPathDocument document = new XPathDocument(@"myFile.xml");
    XPathNavigator navigator = document.CreateNavigator();
    string foundElementContent = 
      navigator.SelectSingleNode("//myElement[position()=1]/text()").ToString();
    
    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();
    
    string elementNameToFind = "myElement";
    XDocument xmlInMemoryDoc = XDocument.Load(@"myFile.xml");
    XElement foundElement = xmlInMemoryDoc.Descendants(elementNameToFind).First();
    
    string elementNameToFind = "myElement";
    XDocument xmlInMemoryDoc = XDocument.Load(@"myFile.xml");
    XElement foundElement =
      (
         from e in xmlInMemoryDoc.Descendants()
         where e.Name == elementNameToFind
         select e
      ).First();
    
    2.

    XPathDocument document = new XPathDocument(@"myFile.xml");
    XPathNavigator navigator = document.CreateNavigator();
    string foundElementContent = 
      navigator.SelectSingleNode("//myElement[position()=1]/text()").ToString();
    
    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();
    
    string elementNameToFind = "myElement";
    XDocument xmlInMemoryDoc = XDocument.Load(@"myFile.xml");
    XElement foundElement = xmlInMemoryDoc.Descendants(elementNameToFind).First();
    
    string elementNameToFind = "myElement";
    XDocument xmlInMemoryDoc = XDocument.Load(@"myFile.xml");
    XElement foundElement =
      (
         from e in xmlInMemoryDoc.Descendants()
         where e.Name == elementNameToFind
         select e
      ).First();
    
    3.

    XPathDocument document = new XPathDocument(@"myFile.xml");
    XPathNavigator navigator = document.CreateNavigator();
    string foundElementContent = 
      navigator.SelectSingleNode("//myElement[position()=1]/text()").ToString();
    
    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();
    
    string elementNameToFind = "myElement";
    XDocument xmlInMemoryDoc = XDocument.Load(@"myFile.xml");
    XElement foundElement = xmlInMemoryDoc.Descendants(elementNameToFind).First();
    
    string elementNameToFind = "myElement";
    XDocument xmlInMemoryDoc = XDocument.Load(@"myFile.xml");
    XElement foundElement =
      (
         from e in xmlInMemoryDoc.Descendants()
         where e.Name == elementNameToFind
         select e
      ).First();
    
    4.

    XPathDocument document = new XPathDocument(@"myFile.xml");
    XPathNavigator navigator = document.CreateNavigator();
    string foundElementContent = 
      navigator.SelectSingleNode("//myElement[position()=1]/text()").ToString();
    
    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();
    
    string elementNameToFind = "myElement";
    XDocument xmlInMemoryDoc = XDocument.Load(@"myFile.xml");
    XElement foundElement = xmlInMemoryDoc.Descendants(elementNameToFind).First();
    
    string elementNameToFind = "myElement";
    XDocument xmlInMemoryDoc = XDocument.Load(@"myFile.xml");
    XElement foundElement =
      (
         from e in xmlInMemoryDoc.Descendants()
         where e.Name == elementNameToFind
         select e
      ).First();
    

    是否尝试按节点的名称和值选择节点?