按标签读取特定的xml值

时间:2016-06-05 10:10:23

标签: c# xml linq

简单形式:reverse of this我目前正在阅读我在程序中创建的xml文件(该部分工作正常),然后将值应用于程序中的基准。示例xml是

<Data>
    <majorItem1>15</majorItem1>
    <majorItem2>22.6</majorItem2>
    <majorItem3>this string</majorItem3>
    <List>
        <items>
            <item1>2</item1>
            <item2>x1</item2>
            <item3>4</item3>
        </items>
        <items>
            <item1>5</item1>
            <item2>x2</item2>
            <item3>28</item3>
        </items>
        <items>
            <item1>12</item1>
            <item2>x3</item2>
            <item3>100</item3>
        </items>
    </List>
</Data>

需要进行解析,并进行具体评估(我确实需要它是那么严格) 我目前的抽象代码是:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace xmlTest {
    public struct things {
        public int item1;
        public string item2;
        public float item3;
    }

    class Program {

        static void Main(string[] args) {
            int majorItem1 = 15;
            int inItem1;
            float majorItem2 = 22.6f;
            float inItem2;
            string majorItem3 = "this string";
            string inItem3;
            List<things> items = new List<things>();
            List<things> reItems = new List<things>();

            things x1 = new things();
            x1.item1 = 2;
            x1.item2 = "x1";
            x1.item3 = 4;
            items.Add(x1);

            things x2 = new things();
            x2.item1 = 5;
            x2.item2 = "x2";
            x2.item3 = 28;
            items.Add(x2);

            things x3 = new things();
            x3.item1 = 12;
            x3.item2 = "x3";
            x3.item3 = 100;
            items.Add(x3);

            XDocument doc = new XDocument(
                new XDeclaration("1.0", "utf=8", "true"),
                new XElement("Data",
                    new XElement("majorItem1", majorItem1),
                    new XElement("majorItem2", majorItem2),
                    new XElement("majorItem3", majorItem3),
                    new XElement("List",
                        from _t in items
                        select new XElement("items",
                            new XElement("item1", _t.item1),
                            new XElement("item2", _t.item2),
                            new XElement("item3", _t.item3)
                        )
                    )
                )
            );
            // portion in question starts here. everthing above is working fine
            Console.WriteLine(doc.ToString());
            XDocument doc2 = new XDocument(doc);
            // null reference exception is thrown here
            inItem1 = Convert.ToInt32(doc2.Element("majorItem1").Value);
            inItem2 = Convert.ToSingle(doc2.Element("majorItem2").Value);
            inItem3 = doc2.Element("majorItem3").Value;
            foreach(XElement xe in doc2.Descendants("items")) {
                things _thing = new things();
                _thing.item1 = Convert.ToInt32(xe.Element("item1").Value);
                _thing.item2 = xe.Element("item2").Value;
                _thing.item3 = Convert.ToSingle(xe.Element("item3").Value);
            }
            Console.WriteLine(doc2.ToString());
            Console.WriteLine("inItem1: " + inItem1.ToString());
            Console.WriteLine("inItem2: " + inItem2.ToString());
            Console.WriteLine("inItem3: " + inItem3);
            foreach(things _t in reItems) {
                Console.WriteLine("item1: " + _t.item1.ToString());
                Console.WriteLine("item2: " + _t.item2);
                Console.WriteLine("item3: " + _t.item3.ToString());
            }

        }
    }
}

在最终的代码中,我将从程序创建的真实文件中读取xml,以填充程序中的所有字段,除了从xml文件导入外。

如果我从赋值中删除了.Value,它就不再给出nullReferenceExceptions,但它会产生垃圾,因为它只是用转换来翻译整个事物。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:3)

仅当XDocument.Element("element_name")<element_name>的根元素时,

XDocument才有效。在您的情况下,目标元素是根元素的子元素。因此,如链接问题的答案所示,您需要通过Root获取目标元素,例如:

inItem1 = (int)doc2.Root.Element("majorItem1");