【问题标题】:XML extracting attributes using XMLDocument使用 XMLDocument 提取 XML 属性
【发布时间】:2011-09-19 13:52:56
【问题描述】:

我正在尝试使用 XMLDocument (DItem >> Title) 解析 xml 元素 下面是我的代码,但不知何故我没有掌握它....有什么帮助吗?

XmlDocument xmldoc = new XmlDocument();
            XmlNamespaceManager xmlns = new XmlNamespaceManager(xdoc.NameTable);
            xmlns.AddNamespace("DItems", "http://namespace.xsd");
            xmldoc.Load(url); 

        var title = xmldoc.SelectNodes("content", xmlns);
        foreach (XmlNode node in title)
        {
            string title = node.Attributes["Title"].Value;
            //this.ddlTitle.Items.Add(new ListItem(title));
        }

这是我的 XML:

    <?xml version='1.0'?>
<root xmlns="http://www.w3.org/2005/Atom">
  <title type="text">title</title>
  <entry>
    <content type="application/xml">
      <Items xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.namespace.xsd">
        <CatalogSource Acronym="ABC" OrganizationName="organization name" />
        <Item Id="28466" CatalogUrl="url">
          <DItem xmlns:content="http://namespace.xsd" TargetUrl="http://index.html" Title="my title1">
            <content:Source Acronym="ABC" OrganizationName="ABC" />
          </DItem>
        </Item>
      </Items>
    </content>
  </entry>
  <entry>
    <content type="application/xml">
      <Items xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.namespace.xsd">
        <CatalogSource Acronym="ABC" OrganizationName="organization name" />
        <Item Id="28466" CatalogUrl="url">
          <DItem xmlns:content="http://namespace.xsd" TargetUrl="http://index.html" Title="my title2">
            <content:Source Acronym="ABC" OrganizationName="ABC" />
          </DItem>
        </Item>
      </Items>
    </content>
  </entry>
  <entry>
    <content type="application/xml">
      <Items xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.namespace.xsd">
        <CatalogSource Acronym="ABC" OrganizationName="organization name" />
        <Item Id="28466" CatalogUrl="url">
          <DItem xmlns:content="http://namespace.xsd" TargetUrl="http://index.html" Title="my title3">
            <content:Source Acronym="ABC" OrganizationName="ABC" />
          </DItem>
        </Item>
      </Items>
    </content>
  </entry> 
</root>

【问题讨论】:

  • 尝试做 xmldoc.SelectSingleNode("title");您正在做的是在“内容” xml 树中搜索,但标题在该树之外。
  • @Roy: 1+ - 谢谢,外面怎么样,我有内容>>>DItems>>>在DItems里面我有Title - 对吗?
  • 对不起,我没注意到那个标题,我以为是外面的。

标签: c# .net xml xpath


【解决方案1】:
var xmldoc = new XmlDocument();
var xmlns = new XmlNamespaceManager(xmldoc.NameTable);
xmlns.AddNamespace("DItems", "http://www.namespace.xsd");
xmldoc.Load(url);

var titleNodes = xmldoc.SelectNodes("//DItems:DItem/@Title", xmlns);

var result = titleNodes.Cast<XmlAttribute>().Select(a => a.Value).ToList();

输出(对象列表):

my title1
my title2
my title3

【讨论】:

  • 我在for loop 中使用的原因是因为我将结果添加到下拉列表中,如我的代码所示...
  • @Abu,有什么问题吗?提供包含多个元素的示例 XML
  • 好的,所以我尝试循环这样的东西 foreach (XmlNode node in result) { string title = node.Attributes["Title"].Value; } 但我收到错误
  • 无法将字符串转换为 xmlnode
  • @Abu, foreach (string title in result) { // title is of type string }
猜你喜欢
  • 2010-10-30
  • 1970-01-01
  • 1970-01-01
  • 2013-08-28
  • 2015-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多