使用xmldocument读取xmlnode

本文关键字:xmlnode 读取 xmldocument 使用 | 更新日期: 2023-09-27 18:11:03

如何从下面的示例XML >>> CatalogItem Id="3212"OrganizationName中读取Id

string xmlfile = @"C:'Users'easwaranp'Desktop'test.xml";
            ArrayList arrResult = new ArrayList();
            string sContent = File.ReadAllText(xmlfile);
            StringReader DocumentReader = new StringReader(sContent);
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(DocumentReader);
            XmlNodeList ReferenceNodes = xmlDoc.GetElementsByTagName("content:Topic");
            foreach (XmlNode Node in ReferenceNodes)
            {                
              string sTopicName = Node.ParentNode.ParentNode.Attributes["OrganizationName"].Value;
            }
            foreach (string s in arrResult)
            {
                Console.WriteLine(s);
            }
            Console.Read();

<content type="application/xml">
    <CatalogItems xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="sitename.xsd">
        <CatalogSource Acronym="ABC" OrganizationName="ABC Corporation" />
        <CatalogItem Id="32122" CatalogUrl="urlname">
            <ContentItem xmlns:content="sitename.xsd" Title="Department 1">
                <content:SelectionSpec ClassList="" ElementList="" />
                <content:Language Value="eng" Scheme="ISO 639-2" />
                <content:Source Acronym="ABC" OrganizationName="ABC Corporation1" />
                <content:Topics Scheme="ABC">
                    <content:Topic TopicId="1" TopicName="Marketing1" />
                    <content:Topic TopicId="11" TopicName="Coverage1" />
                </content:Topics>
            </ContentItem>
        </CatalogItem>
    <CatalogItem Id="3212" CatalogUrl="urlname">
        <ContentItem xmlns:content="sitename.xsd" Title="Department 2">
            <content:SelectionSpec ClassList="" ElementList="" />
            <content:Language Value="eng" Scheme="ISO 639-2" />
            <content:Source Acronym="ABC" OrganizationName="ABC Corporation2" />
            <content:Topics Scheme="ABC">
                <content:Topic TopicId="2" TopicName="Marketing2" />
                <content:Topic TopicId="22" TopicName="Coverage2" />
            </content:Topics>
        </ContentItem>
    </CatalogItem>
    <CatalogItem Id="32132" CatalogUrl="urlname">
        <ContentItem xmlns:content="sitename.xsd" Title="Department 3">
            <content:SelectionSpec ClassList="" ElementList="" />
            <content:Language Value="eng" Scheme="ISO 639-2" />
            <content:Source Acronym="ABC" OrganizationName="ABC Corporation3" />
            <content:Topics Scheme="ABC">
                <content:Topic TopicId="3" TopicName="Marketing3" />
                <content:Topic TopicId="33" TopicName="Coverage3" />
            </content:Topics>
        </ContentItem>
    </CatalogItem>
    </CatalogItems>
</content>

使用xmldocument读取xmlnode

using System;
using System.IO;
using System.Xml;
class GetValue{
    public static void Main(){
        var xmlDoc = new XmlDocument();
        xmlDoc.Load("test.xml");
        var xmlRoot = xmlDoc.DocumentElement;
        var nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
        nsmgr.AddNamespace("content", "sitename.xsd");
        var attr = xmlRoot.SelectSingleNode("descendant::content:Source/@OrganizationName[../../../@Id='3212']", nsmgr);
        if(attr == null)
            Console.WriteLine("not found!");
        else
            Console.WriteLine(attr.Value);
    }
}
输出:

ABC Corporation2

在处理此类任务时,XPath是您的朋友。我从codeproject.com上的教程中改编了以下代码。我不知道它是否工作(或者甚至可以编译),但它应该让你开始在正确的方向。另外,我假设你说的是c#,但如果我误解了你使用的语言,请忽略(你的问题上的语言标签可能会有所帮助)。

using System.Xml;
using System.Xml.XPath;
....
string fileName = "test.xml";
XPathDocument doc = new XPathDocument(fileName);
XPathNavigator nav = doc.CreateNavigator();
// Compile a standard XPath expression
XPathExpression expr; 
idExpr = nav.Compile("/content/CatalogItems/CatalogItem/@id");
XPathNodeIterator iterator = nav.Select(idExpr);
// Iterate on the node set
try
{
  while (iterator.MoveNext())
  {
     XPathNavigator nav2 = iterator.Current.Clone();
     Console.WriteLine("id: " + nav2.Value);
  }
}
catch(Exception ex) 
{
   Console.WriteLine(ex.Message);
}