使用 xmlDocment 读取 xmlnode

发布于 2024-12-09 07:46:17 字数 3203 浏览 1 评论 0原文

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

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>

how can i read an Id from the below sample XML >>> CatalogItem Id="3212" and OrganizationName

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

长安忆 2024-12-16 07:46:17
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

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);
    }
}

OUTPUT:

ABC Corporation2

熟人话多 2024-12-16 07:46:17

当涉及到此类任务时,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);
}

XPath is your friend when it comes to tasks like this. I adapted the following code from a tutorial on codeproject.com. I don't know if it works (or will even compile), but it should get you started in the right direction. Also, I'm assuming your talking about C#, but in case I've misunderstood what language you're using, please disregard (a language tag on your question might help).

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文