使用xmldocument读取xml

时间:2013-08-26 11:21:44

标签: c# xmldocument

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

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

我正在尝试使用c#读取上面的xml,但是我在try catch块中得到null异常可以任何正文建议所需的更改。

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

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

5 个答案:

答案 0 :(得分:7)

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

答案 1 :(得分:3)

您可以使用LINQ to XML从xml中选择所有testcase元素并将其解析为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")
                };

答案 2 :(得分: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正确答案

答案 3 :(得分:0)

您正在尝试阅读属性,而date, namesubject不是属性。它们是子节点。

你的代码应该是这样的

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

答案 4 :(得分:0)

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

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

或者这个:

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