using xmldocument to read xml

2019-02-15 11:34发布

问题:

 <?xml version="1.0" encoding="utf-8" ?> 

  <testcase>
      <date>4/12/13</date>
      <name>Mrinal</name>
      <subject>xmlTest</subject>
  </testcase>

I am trying to read the above xml using c#, But i get null exception in the try catch block can any body suggest the required change.

static void Main(string[] args)
        {        

            XmlDocument xd = new XmlDocument();
            xd.Load("C:/Users/mkumar/Documents/testcase.xml");

            XmlNodeList nodelist = xd.SelectNodes("/testcase"); // get all <testcase> nodes

            foreach (XmlNode node in nodelist) // for each <testcase> node
            {
                CommonLib.TestCase tc = new CommonLib.TestCase();

                try
                {
                    tc.name = node.Attributes.GetNamedItem("date").Value;
                    tc.date = node.Attributes.GetNamedItem("name").Value;
                    tc.sub = node.Attributes.GetNamedItem("subject").Value;

                 }
                catch (Exception e)
                {
                    MessageBox.Show("Error in reading XML", "xmlError", MessageBoxButtons.OK);
                }

........ .....

回答1:

The testcase element has no attributes. You should be looking to it's child nodes:

tc.name = node.SelectSingleNode("name").InnerText;
tc.date = node.SelectSingleNode("date").InnerText;
tc.sub = node.SelectSingleNode("subject").InnerText;

You might process all nodes like this:

var testCases = nodelist
    .Cast<XmlNode>()
    .Select(x => new CommonLib.TestCase()
    {
        name = x.SelectSingleNode("name").InnerText,
        date = x.SelectSingleNode("date").InnerText,
        sub = x.SelectSingleNode("subject").InnerText
    })
    .ToList();


回答2:

You can use LINQ to XML to select all testcase elements from your xml and parse them to TestCase instances:

var xdoc = XDocument.Load("C:/Users/mkumar/Documents/testcase.xml");
var testCases = from tc in xdoc.Descendants("testcase")
                select new CommonLib.TestCase {
                   date = (string)tc.Element("date"),
                   name = (string)tc.Element("name"),
                   sub= (string)tc.Element("subject")
                };

BTW you have only one testcase element currently, which is root of XML file. So, you can do instead:

var tc = XElement.Load("C:/Users/mkumar/Documents/testcase.xml");
var testCase = new CommonLib.TestCase {
                   date = (string)tc.Element("date"),
                   name = (string)tc.Element("name"),
                   sub= (string)tc.Element("subject")
                };


回答3:

private static void Main(string[] args)
{
  XmlDocument xd = new XmlDocument();
  xd.Load("C:\\test1.xml");

  XmlNodeList nodelist = xd.SelectNodes("/testcase"); // get all <testcase> nodes

  foreach (XmlNode node in nodelist) // for each <testcase> node
  {
    try
    {
      var name = node.SelectSingleNode("date").InnerText;
      var date = node.Attributes.GetNamedItem("name").Value;
      var sub = node.Attributes.GetNamedItem("subject").Value;

    }
    catch (Exception e)
    {
      MessageBox.Show("Error in reading XML", "xmlError", MessageBoxButtons.OK);


    }
  }

This will work I have test it @Alex correct answer



回答4:

You are trying to read attributes whereas date, name and subject are not attributes. They are subnodes.

your code should be like this

XmlDocument xd = new XmlDocument();
xd.Load("test.xml");
XmlNodeList nodelist = xd.SelectNodes("/testcase"); // get all <testcase> nodes

foreach (XmlNode node in nodelist) // for each <testcase> node
{
    try
    {                    
          string name = node.SelectSingleNode("name").InnerText;
          string date = node.SelectSingleNode("date").InnerText;
          string sub = node.SelectSingleNode("subject").InnerText;
    }
    catch (Exception ex)
    {
          MessageBox.Show("Error in reading XML", "xmlError", MessageBoxButtons.OK);
    }
}


回答5:

Your Xml do not contain Attributes. date, name and subject - it's Child Nodes of the testcase Node. Try this:

...
tc.name = node["name"].InnerText;
...

or this:

...
tc.name = node.SelectSingleNode("name").InnerText;
...