2011-12-28 89 views
0

我想使用c#讀取以下xml。使用C#讀取包含xmlns屬性的xml節點

<configuration> 
    <configSections> 
    <sectionGroup name="spring"> 
     <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core" /> 
     <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" /> 
    </sectionGroup> 
    </configSections> 
    <spring> 
    <context> 
     <resource uri="config://spring/objects" /> 
    </context> 
    <objects xmlns="http://www.springframework.net"> 
<object></object> 
</objects> 
</spring> 
</configuration> 

它不會識別對象節點的xmlns。

string xmlFile = @"App1.config"; 
XmlDocument xmlDoc = new XmlDocument(); 
xmlDoc.Load(xmlFile); 
XmlNodeList nodeList = xmlDoc.SelectNodes("configuration/spring/objects/object"); 
foreach (XmlElement xmlElement in nodeList) 
{ 
    string id = xmlElement.GetAttribute("id"); //Output - SimulatorControlTCP 
    XmlNodeList xmlPropertyNodeList = xmlElement.SelectNodes("property"); 
    foreach (XmlElement xmlPropertyElement in xmlPropertyNodeList) 
    { 
     id = xmlPropertyElement.GetAttribute("value"); 
     if ((id.Contains("tcp") || id.Contains("http")) && id.Contains("localhost")) 
     { 
      id = id.Replace("localhost","1.1.1.1"); 
      xmlPropertyElement.Attributes[1].Value = id; 
      xmlDoc.Save(xmlFile); 
     } 
    } 

} 

它不會進入foreach循環。如果我刪除xmlns,那麼上面的代碼工作正常。

+0

只需清楚:在調用XmlDocument.Read之後,您確實會得到一個名爲「objects」的XmlNode,但具有空的XmlAttributeCollection? – 2011-12-28 11:18:52

+0

我已添加代碼。代碼在刪除xmlns屬性後起作用 – user660232 2011-12-28 11:29:53

+0

我在調用XmlDocument.Read之後不會獲取對象節點,因爲它包含xmlns屬性 – user660232 2011-12-28 11:40:36

回答

0

一個辦法:

  DataSet ds = new DataSet(); 
      ds.ReadXml(Server.MapPath("YourFile.xml")); 
      GridView1.DataSource = ds; 
      GridView1.DataBind(); 

下一頁方式:

<asp:Xml ID="Xml1" runat="server" DocumentSource="~/XML/XMLFile.xml"> 
    </asp:Xml> 

更多關於第二個方法參見
http://www.codeproject.com/KB/aspnet/ASPNET_20_XMLControl.aspx

0

老問題,但我認爲,很多人都有這個問題像我一樣。

xmlns代表:XML NameSpace。 因此,當它出現在XML中時,您需要使用它來進行搜索。例如,如果要查找像"<url xmlns='sm'>"這樣的元素,則需要指定您正在查找名稱爲"url"和名稱空間"sm"的元素。

爲什麼?對不起,我不能確切地回答原因,但它是這樣工作的。

那麼,爲了解決我的問題,我已經改變了我的代碼使用XmlReader和LinqToXML,什麼使事情「更容易」。

這是XML:

<?xml version="1.0" encoding="UTF-8"?> 
    <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> 
     <url> 
     <loc>http://www.url.com/</loc> 
     <changefreq>daily</changefreq> 
     <priority>1.00</priority> 
     </url> 
     <url> 
     <loc>http://www.url.com/</loc> 
     <changefreq>daily</changefreq> 
     <priority>0.9000</priority> 
     </url> 
     <url> 
     <loc>http://www.url.com/</loc> 
     <changefreq>daily</changefreq> 
     <priority>0.9000</priority> 
     </url> 
     <url> 
     <loc>http://www.url.com/</loc> 
     <changefreq>daily</changefreq> 
     <priority>0.9000</priority> 
     </url> 
    </urlset> 

這是我以前看過它的代碼:

 XDocument xmlDoc = XDocument.Load("sitemap.xml"); 

     if (xmlDoc.Elements().Count() > 0) 
     { 
      using (MemoryStream memStream = new MemoryStream()) 
      { 
       xmlDoc.Save(memStream); 
       memStream.Position = 0; 

       using (XmlReader reader = XmlReader.Create("sitemap.xml", new XmlReaderSettings() { IgnoreWhitespace = true })) 
       { 
        reader.MoveToContent(); 
        while (reader.Read()) 
        { 
         if (reader.NodeType == XmlNodeType.Element && reader.LocalName.Equals("url", StringComparison.OrdinalIgnoreCase)) 
         { 
          XElement el = XNode.ReadFrom(reader) as XElement; 

          if (el == null) 
           continue; 

          XName loc = XName.Get("loc", el.Name.Namespace.NamespaceName); 
          XName changefreq = XName.Get("changefreq", el.Name.Namespace.NamespaceName); 
          XName priority = XName.Get("priority", el.Name.Namespace.NamespaceName); 

          var elLoc = el.Element(loc); 
          var elChangeFreq = el.Element(changefreq); 
          var elPriority = el.Element(priority); 
         } 
        } 
       } 
      } 
     } 

希望這有助於。