简体   繁体   中英

Reading xml node containing xmlns attribute using C#

I want to read the following xml using c#.

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

It wont identify xmlns from objects node.

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

}

It won't go inside foreach loop. If I remove xmlns , then the above code works fine.

one way:

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

Next way:

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

For more about 2nd method see
http://www.codeproject.com/KB/aspnet/ASPNET_20_XMLControl.aspx

Old question but I think that lot of people have this problem, like I did.

The xmlns stand for: XML NameSpace. So, when it is in present in the XML you need to use it to search. For example, if you want to find an element like "<url xmlns='sm'>" , you would need to especify that you're looking for an element that has name "url" and namespace "sm" .

Why? I'm sorry I can't answer exactly why, but it works like this.

Well, to solve my problem I've changed my code to use XmlReader and LinqToXML, what make things "much easier".

This is the 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>

And this is the code that I used to read it:

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

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM