Home > Net >  Reading a XML file using C# not working for some xml files
Reading a XML file using C# not working for some xml files

Time:04-22

I need to read following xml file. I've used XML and LINQ but neither of them show any values.I can't find any mistakes on the code. I've followed This example it's working fine with XML that shown there.

<dataSet>
<transactions>
<trans>1</trans>
<Amount>1000</Amount>
<Name>1000</Name>
<Type>Income</Type>
<Date>2022-04-21T00:00:00 05:30</Date>
</transactions>
</dataSet>

I've use this code.

using System;
using System.Xml;

namespace ReadXMLInCsharp
{
    class Program
    {
        static void Main(string[] args)
        {
            //create XMLDocument object
            XmlDocument xmlDoc = new XmlDocument();

            //returns url of main directory which contains "/bin/Debug"
            var url = System.IO.Path.GetDirectoryName(
    System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);

            //correction in path to point it in Root directory
            var mainpath = ("F:\\Education\\Test\\new.xml");
            //load xml file
            xmlDoc.Load(mainpath);

            XmlNodeList nodeList = xmlDoc.DocumentElement.SelectNodes("/dataSet/transactions");

            
            var NodeStr = "";

            foreach (XmlNode node in nodeList)
            {
                NodeStr = NodeStr   "\nTransaction "   node.SelectSingleNode("trans").InnerText;
               


            }
            Console.WriteLine(NodeStr);
        }
    }
}

CodePudding user response:

Your xml DataSet node has a namespace attribute, so you'll either need to remove it or use XmlNamespaceManager to handle it. Here is a fiddle with both:

https://dotnetfiddle.net/EOXtBN

In the first, I load the xml into a string, then use .Replace:

xmlDoc.LoadXml(getXML().Replace(" xmlns='tempuri.org/DataSet.xsd'", ""));

Probably not optimal, because that namespace is probably there for a reason, but it's possible this could be an option for you. It's the one I got to work first.

Second, I use XmlNamespaceManager to handle the parsing. It doesn't add that much overhead:

xmlDoc.LoadXml(getXML());
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("ns", "tempuri.org/DataSet.xsd");
        
nodeList = xmlDoc.DocumentElement.SelectNodes("//ns:DataSet//ns:transactions", nsmgr); 
foreach (XmlNode node in nodeList) {
  NodeStr = NodeStr   "\nTransaction "   node.SelectSingleNode("ns:trans", nsmgr).InnerText;       
}

Also, bear in mind that you can just include ns:trans in the xpath for the nodelist, like so:

nodeList = xmlDoc.DocumentElement.SelectNodes("//ns:DataSet//ns:transactions//ns:trans", nsmgr); 
foreach (XmlNode node in nodeList) {
  NodeStr = NodeStr   "\nTransaction "   node.InnerText;       
}
Console.WriteLine(NodeStr);

And you can use = to clean it up a little more, too:

nodeList = xmlDoc.DocumentElement.SelectNodes("//ns:DataSet//ns:transactions//ns:trans", nsmgr); 
foreach (XmlNode node in nodeList) {
  NodeStr  = "\nTransaction "   node.InnerText;       
}
Console.WriteLine(NodeStr);

Let me know if this works for you.

CodePudding user response:

Your XML file have and you are parsing it via dataset. "s" is in lowercasein your code. set it to XmlNodeList nodeList = xmlDoc.DocumentElement.SelectNodes("/dataSet/transactions");

Also check the path to your XML is correct.

  • Related