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

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

4

5 回答 5

8

testcase元素没有属性。您应该查看它的子节点:

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

您可以像这样处理所有节点:

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();
于 2013-08-26T11:28:25.007 回答
3

您可以使用 LINQ to XMLtestcase从您的 xml 中选择所有元素并将它们解析为TestCase实例:

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

顺便说一句,您目前只有一个testcase元素,即 XML 文件的根。因此,您可以改为:

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")
                };
于 2013-08-26T11:28:24.293 回答
2
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);


    }
  }

这会起作用我已经测试过@Alex正确答案

于 2013-08-26T11:32:00.413 回答
0

您正在尝试读取属性而date, name不是subject属性。它们是子节点。

你的代码应该是这样的

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);
    }
}
于 2013-08-26T11:33:26.950 回答
0

您的 Xml 不包含属性。日期、名称和主题 - 它是测试用例节点的子节点。尝试这个:

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

或这个:

...
tc.name = node.SelectSingleNode("name").InnerText;
...
于 2013-08-26T11:34:15.567 回答