2011-10-11 107 views
0

我怎麼可以閱讀下面的示例XML >>> CatalogItem Id="3212"IdOrganizationName閱讀使用xmlDocment

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> 
+1

你僅限於使用'XMLDocument'或者你可以使用'XDocument'? –

+0

我打開使用任何api,只要它與我的另一半的代碼...這就是爲什麼我說使用xmldocument –

回答

0
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

0

XPath是你的朋友,當談到這樣的任務的XMLNode。我修改了a tutorial on 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); 
}