Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.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# 在.Net中使用Linq解析没有根元素的xml元素_C#_.net_Xml_Linq_Xpath - Fatal编程技术网

C# 在.Net中使用Linq解析没有根元素的xml元素

C# 在.Net中使用Linq解析没有根元素的xml元素,c#,.net,xml,linq,xpath,C#,.net,Xml,Linq,Xpath,我有一个似乎无法查询的xml包,因为它不包含根元素。不幸的是,xml有效负载无法更改,因此我无法使用这种方法。我想知道是否有好的方法来改变我的例子。我也尝试过使用Desendantself,但没能成功。谢谢你的帮助 string xml = @"<book number='3'> <description>Test</description> <chapter num

我有一个似乎无法查询的xml包,因为它不包含根元素。不幸的是,xml有效负载无法更改,因此我无法使用这种方法。我想知道是否有好的方法来改变我的例子。我也尝试过使用Desendantself,但没能成功。谢谢你的帮助

    string xml = @"<book number='3'>
                    <description>Test</description>
                    <chapter number='3-1-1'>
                        <description>Test</description>
                    </chapter>
                    <chapter number='3-1-2'>
                        <description>Test</description>
                    </chapter>
                    </book>";
stringxml=@”
试验
试验
试验
";
下面是我的代码示例:

                    XElement element= XElement.Parse(xml);
                    List<Book> books = ( from t in element.Descendants("book")
                        select new Book
                        {
                            number = (String)t.Attribute("number"),
                            description = (String)t.Element("description").Value,

                            // Load the chapter information
                            chapters = t.Elements("chapter")
                                .Select(te => new Chapter
                            {
                                number = (String)te.Attribute("number"),
                                description = (String)t.Element("description").Value
                            }).ToList(),                                    
                        }).ToList();

            foreach(var d in books)
            {
                Console.WriteLine(String.Format("number = {0}: description = {1}",d.number,d.description));
                foreach(var c in d.chapters)
                    Console.WriteLine(String.Format("number = {0}: description = {1}",c.number,c.description));
            }
XElement元素=XElement.Parse(xml);
List books=(从元素的子体(“book”)中的t开始)
选择新书
{
number=(String)t.Attribute(“number”),
description=(字符串)t.Element(“description”).Value,
//加载章节信息
章节=t.元素(“章节”)
.选择(te=>新章节
{
number=(字符串)te.Attribute(“number”),
description=(字符串)t.Element(“description”).Value
}).ToList(),
}).ToList();
foreach(账簿中的变量d)
{
WriteLine(String.Format(“number={0}:description={1}”,d.number,d.description));
foreach(d.章中的var c)
WriteLine(String.Format(“number={0}:description={1}”,c.number,c.description));
}
这是我的类对象:

public class Book
{
    public String number { get; set; }
    public String description { get; set; }
    public List<Chapter> chapters { get; set; }
}

public class Chapter
{
    public String number { get; set; }
    public String description { get; set; }
}
公共课堂教材
{
公共字符串编号{get;set;}
公共字符串说明{get;set;}
公共列表章节{get;set;}
}
公共课章节
{
公共字符串编号{get;set;}
公共字符串说明{get;set;}
}

只需更改
XElement元素=XElement.Parse(xml)
to
var元素=XDocument.Parse(xml)。在本例中,您将获得一个
XDocument
实例,它有一个
book
子体。如果使用
XElement元素=XElement.Parse(xml)
您当前的元素将是
book
,它没有任何
book
子元素。

book
是您的根元素。如果在一个字符串中有多个
book
元素,只需将其包装在一个伪元素中(例如
“+xml+”
),这是个好主意。但是如果book是根元素,为什么Linq查询不能正常工作。因为它找不到book元素。正在使用element.substands(“book”)不正确?否,因为本例中的
元素
book
元素。它没有类型为
book
的decentant。