1

i am trying to read xml data using xml reader , the xml file include alot of prefixes so i included the namespaces in an XmlNamespaceManager , example code

using (XmlReader reader = new XmlTextReader(fileName))
{
    XmlNamespaceManager nsmanager = new XmlNamespaceManager(reader.NameTable);
    nsmanager.AddNamespace("s", "http://www.google.com/shopping/api/schemas/2010");
    while (reader.Read())
    {
        switch (reader.Name)
        {
            case "s:name":
                Console.WriteLine(reader.Name);
                break;
            case "s:condition": 
                Console.WriteLine(reader.Name);
                break;
        }
    }
}

its outputing empty lines, is this the right way to include the namespaces?

in vb.net i imported the namespaces as

Imports <xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
Imports <xmlns:msxsl="urn:schemas-microsoft-com:xslt">
Imports <xmlns:rh="ReportHub">
Imports <xmlns="http://www.w3.org/2005/Atom">
Imports <xmlns:gd="http://schemas.google.com/g/2005">
Imports <xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/">
Imports <xmlns:s="http://www.google.com/shopping/api/schemas/2010">
7
  • Can you show us a snippet of the input XML file? Commented Mar 2, 2013 at 23:46
  • @JeremyThompson just did, the complete xml file contains multiple snippets like the above, complete xml files a couple of the above xml and namespaces i included in the vb.net code snippet. Commented Mar 2, 2013 at 23:51
  • 2
    Do you particularly have to use XmlReader? I usually find that LINQ to XML makes working with namespaces much simpler. Commented Mar 2, 2013 at 23:56
  • @JonSkeet i am not really sure but speed is important since i will be parsing multiple files per second and xmlreader is supposed to be faster, things were really simple in vb.net! Commented Mar 3, 2013 at 0:03
  • @user1590636: Until you've benchmarked anything and proved that the difference between LINQ to XML and XmlReader is significant, I wouldn't assume that you have to use XmlReader. Commented Mar 3, 2013 at 0:18

1 Answer 1

1

problem solved by using reader.ReadElementString rather than reader.Value

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.