2011-12-28 13 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? –

+0

私はコードを追加しました。コードはxmlns属性を削除した後に動作します – user660232

+0

xmlns属性が含まれているため、XmlDocument.Readを呼び出した後でオブジェクトノードを取得できません – user660232

答えて

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名前空間を表します。 それが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); 
         } 
        } 
       } 
      } 
     } 

は、この情報がお役に立てば幸いです。

関連する問題