Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.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# C Linq到XML读取带有属性的多个标记_C#_Xml_Linq_Parsing - Fatal编程技术网

C# C Linq到XML读取带有属性的多个标记

C# C Linq到XML读取带有属性的多个标记,c#,xml,linq,parsing,C#,Xml,Linq,Parsing,我正在尝试使用LINQtoXML读取XML文件,但似乎不知道如何读取 我有一个XML文件: <?xml version="1.0" encoding="utf-8" ?> <Thing> <Objects> <MyTag name="" num="2"> <Date month="May" day="2" year="2006" /> </MyTag>

我正在尝试使用LINQtoXML读取XML文件,但似乎不知道如何读取

我有一个XML文件:

<?xml version="1.0" encoding="utf-8" ?>
<Thing>
    <Objects>
        <MyTag name="" num="2">
            <Date month="May" day="2" year="2006" />
        </MyTag>

        <MyTag name="" num="4">
            <Date month="May" day="22" year="2012" />
        </MyTag>

        <MyTag name="" num="2">
            <Date month="May" day="11" year="2034" />
        </MyTag>
    </Objects>
</Thing>
如何获取日期标记和属性?不确定如何获取下一个节点

我尝试在foreach循环中打印TagName和TagNum,如下所示:

foreach(string value in query)
{
    Console.WriteLine(value.TagName + " " + value.TagNum); 
}
然而,我得到了一个错误的说法

CS0030  Cannot convert type '<anonymous type: string TagName, string TagNum>' to 'string'
像这样更改它,var类型而不是string

要获取日期,只需使用.Element获取子项:

from thing in document.Root.Descendants("Objects")
let date = thing.Element("Date")
select new
{
    TagName = (string)thing.Attribute("name"),
    TagNum = (string)thing.Attribute("num"),

    DateMonth = (string)date?.Attribute("month"),
    DateDay = (string)date?.Attribute("day"),
    DateYear = (string)date?.Attribute("year"),
};
您的foreach语句未编译,因为当查询集合是new{}返回的匿名类型时,您正在请求字符串。您将希望使用var而不是字符串:

使用ElementName方法

var query = 
    document.Root
            .Descendants("Objects")
            .Select(obj => new
            {
                TagName = thing.Attribute("name").Value,
                TagNum = thing.Attribute("num").Value, 
                Year = thing.Element("Date").Attribute("year").Value,
                Month = thing.Element("Date").Attribute("month").Value,
                Day = thing.Element("Date").Attribute("day").Value  
            };

在querytank中使用foreach var值。我认为查询中的所有内容都是字符串。谢谢您的解释。从中我们学到了很多。
from thing in document.Root.Descendants("Objects")
let date = thing.Element("Date")
select new
{
    TagName = (string)thing.Attribute("name"),
    TagNum = (string)thing.Attribute("num"),

    DateMonth = (string)date?.Attribute("month"),
    DateDay = (string)date?.Attribute("day"),
    DateYear = (string)date?.Attribute("year"),
};
foreach(var value in query)
{
    Console.WriteLine(value.TagName + " " + value.TagNum); 
}
var query = 
    document.Root
            .Descendants("Objects")
            .Select(obj => new
            {
                TagName = thing.Attribute("name").Value,
                TagNum = thing.Attribute("num").Value, 
                Year = thing.Element("Date").Attribute("year").Value,
                Month = thing.Element("Date").Attribute("month").Value,
                Day = thing.Element("Date").Attribute("day").Value  
            };