Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过标记读取特定的xml值_C#_Xml_Linq - Fatal编程技术网

C# 通过标记读取特定的xml值

C# 通过标记读取特定的xml值,c#,xml,linq,C#,Xml,Linq,简单形式:我目前正在将我创建的xml文件读入我的程序(该部分工作正常),然后将这些值应用到我的程序中的基准。示例xml是 <Data> <majorItem1>15</majorItem1> <majorItem2>22.6</majorItem2> <majorItem3>this string</majorItem3> <List> <item

简单形式:我目前正在将我创建的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>

15
22.6
这根绳子
2.
x1
4.
5.
x2
28
12
x3
100
需要对其进行解析,并进行具体评估(我确实需要它如此严格) 我当前的抽象代码是:

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

        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Xml.Linq;
命名空间xmlTest{
公共结构物{
公共项目1;
公共字符串项2;
公共浮动项目3;
}
班级计划{
静态void Main(字符串[]参数){
国际专业1=15;
int inItem1;
浮点数2=22.6f;
浮动初始化2;
string-majorItem3=“此字符串”;
字符串inItem3;
列表项=新列表();
List reItems=新列表();
事物x1=新事物();
x1.1=2;
x1.item2=“x1”;
x1.3=4;
增加(x1);
事物x2=新事物();
x2.1=5;
x2.item2=“x2”;
x2.3=28;
增加(x2);
事物x3=新事物();
x3.1项=12;
x3.item2=“x3”;
x3.3项=100;
增加项目(x3);
XDocument doc=新XDocument(
新的XDeclaration(“1.0”,“utf=8”,“真”),
新XElement(“数据”,
新XElement(“majorItem1”,majorItem1),
新XElement(“majorItem2”,majorItem2),
新XElement(“majorItem3”,majorItem3),
新XElement(“列表”,
来自项目中的
选择新元素(“项目”,
新XElement(“第1项”、“第1项”),
新XElement(“第2项”、“第2项”),
新XElement(“第3项”,“第3项”)
)
)
)
);
//有问题的部分从这里开始。上面的一切都很好
Console.WriteLine(doc.ToString());
XDocument doc2=新XDocument(doc);
//此处引发空引用异常
inItem1=Convert.ToInt32(doc2.Element(“majorItem1”).Value);
inItem2=Convert.ToSingle(doc2.Element(“majorItem2”).Value);
inItem3=doc2.Element(“majorItem3”).Value;
foreach(doc2.substands(“items”)中的XElement xe){
事物=新事物;
_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());
WriteLine(“inItem1:+inItem1.ToString());
WriteLine(“inItem2:+inItem2.ToString());
Console.WriteLine(“inItem3:+inItem3”);
foreach(在reItems中的事物){
Console.WriteLine(“item1:+_t.item1.ToString());
Console.WriteLine(“item2:+\u t.item2”);
Console.WriteLine(“item3:+\t.item3.ToString());
}
}
}
}
在最后的代码中,我将从一个真实的文件中读取xml,该文件是程序创建的,用于填充程序中的字段,除了从xml文件导入之外,所有内容都已就绪

如果我从赋值中删除.Value,它将不再提供nullReferenceExceptions,而是提供垃圾,因为它只是用强制转换转换整个内容

任何帮助都将不胜感激。

XDocument.Element(“Element\u name”)
仅当
XDocument
的根元素时才起作用。在您的例子中,目标元素是根元素的子元素。因此,如链接问题的答案所示,您需要通过
获取目标元素,例如:

inItem1 = (int)doc2.Root.Element("majorItem1");
XDocument.Element(“Element_name”)
仅在
XDocument
的根元素时有效。在您的例子中,目标元素是根元素的子元素。因此,如链接问题的答案所示,您需要通过
获取目标元素,例如:

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

您确定找到了元素吗?因为我没有看到
列表
标记anywhere@KevinWallisOP的XML中
的父元素?您确定找到了这些元素吗?因为我没有看到
列表
标记anywhere@KevinWallisOP的XML中
的父元素?作为对answer:syntax的补充,并使用添加列表项作为XElement.element(“元素名称”)作为对answer:syntax的补充(只要将结构添加到列表中),效果良好,使用添加列表项作为XElement.Element(“Element_name”)可以很好地工作(只要将结构添加到列表中)