C# 如何在XmlNode中获取文本

C# 如何在XmlNode中获取文本,c#,xml,xmlnodelist,C#,Xml,Xmlnodelist,如何获取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

如何获取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
}
XmlNodeList nodest nodes=rootNode.SelectNodes(“后代::*”);
for(int i=0;i

我最终想做的是对每个节点中的文本预先输入“HELP:”。

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

最简单的方法可能是迭代节点的所有直接子节点(使用
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使用起来会更好。)

在节点的子节点中搜索具有
文本
节点类型
,并使用该节点的
属性

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

检查此项

您还可以检查在编写“reader”时得到的选项

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>

圣马丁
SX
斯洛伐克
SK
和读者真正的基本,但快速

 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;  
XmlTextReader=newxmltextreader(“c:/countryCodes.xml”);
List countriesList=新列表();
国家=新国家();
bool first=false;
while(reader.Read())
{
开关(reader.NodeType)
{
case XmlNodeType.Element://节点是一个元素。
如果(reader.Name==“ISO_3166-1_条目”)国家/地区=新国家/地区();
打破
case XmlNodeType.Text://显示每个元素中的文本。
if(first==false)
{
第一个=正确;
country.Name=reader.Value;
}
其他的
{
country.Code=reader.Value;
国家列表。添加(国家);
第一个=假;
}                       
打破
}        
}
返回国名单;

代码中的注释明确表示OP不希望在所有子节点中获得连接的文本,这是InnerText所做的,而不是我想要的。请参阅文档:“获取或设置节点及其所有子节点的串联值。”
ChildNodes
是一个
XmlNodeList
,它实现了
IEnumerable
的非通用版本。因此,您需要明确上面循环中
子对象的类型,即
foreach(node.ChildNodes中的XmlNode-child)