【问题标题】:XmlDocument get sub itemsXmlDocument 获取子项
【发布时间】:2015-02-02 08:42:21
【问题描述】:

我正在尝试从此 XML 文档中获取子项:

此处为 XML 代码:

<RUNNABLES>
  <RUNNABLE-ENTITY UUID="1661dcae-c4b9-4f47-8b68-cb99c677bdd6">
    <SHORT-NAME>RCtApElg_mlm</SHORT-NAME>
      <DESC>
    <L-2 L="FOR-ALL">The unique runnable entity of the Main Lights SW Component</L-2>
  </DESC>
</RUNNABLES>

到目前为止,这是我所做的:

这里是C#代码

XmlDocument xmlReader = new XmlDocument();
xmlReader.PreserveWhitespace = false;
xmlReader.Load(strfilename);

XmlNodeList elemList = xmlReader.SelectNodes("/RUNNABLES/RUNNABLE-ENTITY/SHORT-NAME");

如何指定读取 SHORT-NAME 标记并获得“RCtApElg_mlm”值作为结果?

【问题讨论】:

  • 改用SelectSingleNode怎么样?

标签: c# parsing xmldocument


【解决方案1】:

我认为你需要使用:
.Value
XmlNodeList elemList = xmlReader.SelectNodes("/RUNNABLES/RUNNABLE-ENTITY/SHORT-NAME").Value;

【讨论】:

    【解决方案2】:

    如果您修复了示例 xml(它错过了 RUNNABLE-ENTITY 的关闭标记),这应该可以工作:

    XmlDocument xmlReader = new XmlDocument();
    xmlReader.PreserveWhitespace = false;
    xmlReader.Load(strfilename);
    
    XmlNodeList elemList = xmlReader.SelectNodes("//RUNNABLES/RUNNABLE-ENTITY/SHORT-NAME");
    string value = elemList.Item(0).InnerText;
    

    【讨论】:

      【解决方案3】:

      这就是我所做的:

                          XmlNode node = xmlReader.DocumentElement.FirstChild;
                          XmlNodeList lstPackage = node.ChildNodes;
      
                          for (int i = 0; i < 2; i++)
                          {
                              //MessageBox.Show(lstPackage[i].Name.ToString());
                              if (lstPackage[i].Name == "AR-PACKAGE")
                              {
                                  aux++;
                              }
                              if(aux==2)
                              {
                                  aux = 0;
                                  XmlNodeList lstShortname = lstPackage[i].FirstChild.ChildNodes;
                                  // Display the value of its first child node
                                  for (int j = 0; j < lstShortname.Count; j++)
                                      MessageBox.Show(lstShortname[j].InnerText);
                              }
                          }
      

      不知道它是否是最好的选择,但我得到了结果。

      谢谢!

      【讨论】:

        猜你喜欢
        • 2017-09-06
        • 1970-01-01
        • 2021-09-30
        • 2015-04-13
        • 1970-01-01
        • 1970-01-01
        • 2013-07-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多