Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.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

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# Linq到XML检索单个节点_C#_Xml_Linq_Linq To Xml - Fatal编程技术网

C# Linq到XML检索单个节点

C# Linq到XML检索单个节点,c#,xml,linq,linq-to-xml,C#,Xml,Linq,Linq To Xml,我有以下XML: <?xml version="1.0" encoding="UTF-8"?> <game> <name>Space Blaster</name> <description></description> <version>1</version> <fullscreen>false</fullscreen> <widt

我有以下XML:

<?xml version="1.0" encoding="UTF-8"?>
<game>
    <name>Space Blaster</name>
    <description></description>
    <version>1</version>
    <fullscreen>false</fullscreen>
    <width>640</width>
    <height>640</height>
    <c2release>6900</c2release>
</game>

但是我似乎不能接受任何这样的值(intellisense讨厌它!)有人能告诉我它是如何完成的吗?

我想你只需要得到元素的值:

string val = doc.Descendants("game")
    .Select(x => x.Element("name").Value).FirstOrDefault();

作为上述操作的先决条件,因此intellisense会选择它,请确保导入
System.Linq
System.Xml.Linq
名称空间。

我认为您只需要获取元素的值:

string val = doc.Descendants("game")
    .Select(x => x.Element("name").Value).FirstOrDefault();

作为上述操作的先决条件,因此intellisense会选择它,请确保导入
System.Linq
System.Xml.Linq
名称空间。

这将获得所有带有“game”标签的子代 您只需要FirstOrDefault()来获取唯一的记录(如果它存在)

             var q = from c in loaded.Descendants("game")
                select new { Name = c.Element("name").Value
                             Description = c.Element("description").Value
                };

             q.FirstOrDefault();

这将获得所有带有“游戏”标签的后代 您只需要FirstOrDefault()来获取唯一的记录(如果它存在)

             var q = from c in loaded.Descendants("game")
                select new { Name = c.Element("name").Value
                             Description = c.Element("description").Value
                };

             q.FirstOrDefault();

对于提取名称节点的值,您的查询实际上是正确的(为我测试并工作过)-您正在执行的
(string)
转换将名称节点的值提取为string,而不是将节点对象本身提供给您,这是Linq to Xml中内置的快捷方式之一。剩下的就是打印出名称:

Response.Write(q);

对于提取名称节点的值,您的查询实际上是正确的(为我测试并工作过)-您正在执行的
(string)
转换将名称节点的值提取为string,而不是将节点对象本身提供给您,这是Linq to Xml中内置的快捷方式之一。剩下的就是打印出名称:

Response.Write(q);
够了。还是为了避免演员阵容

var q = (from c in loaded.Descendants("game") select c.Element("name").Value).SingleOrDefault();
Console.WriteLine(q);
够了。还是为了避免演员阵容

var q = (from c in loaded.Descendants("game") select c.Element("name").Value).SingleOrDefault();
Console.WriteLine(q);