【问题标题】:How to get text inside an XmlNode如何在 XmlNode 中获取文本
【发布时间】:2011-06-07 14:21:06
【问题描述】:

如何获取 XmlNode 中的文本?见下文:

XmlNodeList nodes = rootNode.SelectNodes("descendant::*");
for (int i = 0; i < nodes.Count; i++)
{
    XmlNode node = nodes.Item(i);

    //TODO: Display only the text of only this node, 
   // not a concatenation of the text in all child nodes provided by InnerText
}

而我最终想要做的是在每个节点的文本中添加“HELP:”。

【问题讨论】:

    标签: c# xml xmlnodelist


    【解决方案1】:

    最简单的方法可能是遍历节点的所有直接子节点(使用ChildNodes)并测试每个节点的NodeType 以查看它是Text 还是CDATA。不要忘记可能有多个文本节点。

    foreach (XmlNode child in node.ChildNodes)
    {
        if (child.NodeType == XmlNodeType.Text ||
            child.NodeType == XmlNodeType.CDATA)
        {
            string text = child.Value;
            // Use the text
        }
    }
    

    (仅供参考,如果您可以使用 .NET 3.5,则 LINQ to XML 会很多更好用。)

    【讨论】:

    • ChildNodes 是一个XmlNodeList,它实现了IEnumerable 的非泛型版本。因此,您需要明确说明上述循环中 child 的类型 - 即 foreach (XmlNode child in node.ChildNodes)
    【解决方案2】:

    在节点的子节点中搜索具有NodeTypeText 的节点,并使用该节点的Value 属性。

    请注意,您还可以使用 text() 节点类型测试选择带有 XPath 的文本节点。

    【讨论】:

      【解决方案3】:

      你可以读取 xmlnode 的 InnerText 属性 阅读node.InnerText

      【讨论】:

      • 代码中的注释明确表示 OP 想要在所有子节点中获取连接文本,这就是 InnerText 所做的。
      • 不是我要找的。请参阅文档:“获取或设置节点及其所有子节点的 连接 值。”
      【解决方案4】:

      检查一下

      你也可以检查当你写“阅读器”时你得到什么选项。

      xml文件

      <?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
      <ISO_3166-1_List_en xml:lang="en">
         <ISO_3166-1_Entry>
            <ISO_3166-1_Country_name>SINT MAARTEN</ISO_3166-1_Country_name>
            <ISO_3166-1_Alpha-2_Code_element>SX</ISO_3166-1_Alpha-2_Code_element>
         </ISO_3166-1_Entry>
         <ISO_3166-1_Entry>
            <ISO_3166-1_Country_name>SLOVAKIA</ISO_3166-1_Country_name>
            <ISO_3166-1_Alpha-2_Code_element>SK</ISO_3166-1_Alpha-2_Code_element>
         </ISO_3166-1_Entry>
      </ISO_3166-1_List_en>
      

      阅读器非常基本但速度很快

       XmlTextReader reader = new XmlTextReader("c:/countryCodes.xml");
            List<Country> countriesList = new List<Country>();
            Country country=new Country();
            bool first = false;
            while (reader.Read())
            {
              switch (reader.NodeType)
              {
                case XmlNodeType.Element: // The node is an element.
                  if (reader.Name == "ISO_3166-1_Entry") country = new Country();
                  break;
                case XmlNodeType.Text: //Display the text in each element.
                  if (first == false)
                  {
                    first = true;
                    country.Name = reader.Value;
                  }
                  else
                  {
                    country.Code = reader.Value;
                    countriesList.Add(country);
                    first = false;
                  }                       
                  break;          
              }        
            }
            return countriesList;  
      

      【讨论】:

        猜你喜欢
        • 2021-07-30
        • 2012-09-18
        • 1970-01-01
        • 1970-01-01
        • 2010-09-19
        • 1970-01-01
        • 1970-01-01
        • 2012-02-11
        • 2017-08-27
        相关资源
        最近更新 更多