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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.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 - Fatal编程技术网

C# 从xml中提取数据

C# 从xml中提取数据,c#,xml,C#,Xml,我正试图从xml文件中提取数据 <root response="True"> <movie title="True Grit" year="1969" rated="N/A" released="11 Jun 1969" runtime="128 min" genre="Adventure, Western, Drama" director="Henry Hathaway" writer="Charles Portis (novel), Marguerite Roberts (

我正试图从xml文件中提取数据

<root response="True">
<movie title="True Grit" year="1969" rated="N/A" released="11 Jun 1969" runtime="128 min" genre="Adventure, Western, Drama" director="Henry Hathaway" writer="Charles Portis (novel), Marguerite Roberts (screenplay)" actors="John Wayne, Glen Campbell, Kim Darby, Jeremy Slate" plot="A drunken, hard-nosed U.S. Marshal and a Texas Ranger help a stubborn young woman track down her father's murderer in Indian territory." language="English" country="USA" awards="Won 1 Oscar. Another 7 wins & 5 nominations." poster="http://ia.media-imdb.com/images/M/MV5BMTYwNTE3NDYzOV5BMl5BanBnXkFtZTcwNTU5MzY0MQ@@._V1_SX300.jpg" metascore="N/A" imdbRating="7.4" imdbVotes="26,487" imdbID="tt0065126" type="movie" tomatoMeter="90" tomatoImage="certified" tomatoRating="7.9" tomatoReviews="48" tomatoFresh="43" tomatoRotten="5" tomatoConsensus="N/A" tomatoUserMeter="83" tomatoUserRating="3.8" tomatoUserReviews="24,949" DVD="21 Mar 2000" BoxOffice="N/A" Production="Paramount Home Video" Website="N/A"/>
</root>
xml文件已加载,但我在messagebox上得到空值 如何从每个节点获取值

year
movie
元素的属性:

year
movie
元素的属性:


年份
不是一个元素,它是一个
属性
,请尝试以下操作:

doc.Descendants("movie").First().Attribute("year").Value;

年份
不是一个元素,它是一个
属性
,请尝试以下操作:

doc.Descendants("movie").First().Attribute("year").Value;

第二个问题的答案是
var poster=(string)doc.Root.Element(“movie”).Attribute(“poster”)您可以看到XAttribute的字符串表示。您应该检索属性值。注意-在原始问题中添加问题不好。如果您还有另一个问题,那么只需创建新的单独问题。多谢各位,我发现第二个问题的答案是
var poster=(string)doc.Root.Element(“movie”).Attribute(“poster”)您可以看到XAttribute的字符串表示。您应该检索属性值。注意-在原始问题中添加问题不好。如果您还有其他问题,那么只需创建新的单独问题。多谢大家的解答。注意:对于这个示例xml,您不需要在所有电影元素上创建枚举器,因为只有一个。另外,如果属性不存在,直接检索值可能会引发异常(在本例中转换为nullable类型更好)注意-对于这个示例xml,您不需要在所有电影元素上创建枚举数,因为只有一个。若属性不存在,直接检索值也可能引发异常(在这种情况下,转换为nullable类型更好)
doc.Descendants("movie").First().Attribute("year").Value;