Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.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属性值 acc1 acc2 acc3 acc4_C#_Asp.net_Xml - Fatal编程技术网

C# 获取子节点xml属性值 acc1 acc2 acc3 acc4

C# 获取子节点xml属性值 acc1 acc2 acc3 acc4,c#,asp.net,xml,C#,Asp.net,Xml,例如,如果我想获取每个元素的值: <test> <acc id="1"> acc1 </acc> <acc id="2"> acc2 </acc> <acc id="3"> acc3 </acc> <acc id="4"> acc4 </acc> </test> var iAccs=xdoc.substands(“test”).Elements

例如,如果我想获取每个
元素的值:

<test>
    <acc id="1"> acc1 </acc>
    <acc id="2"> acc2 </acc>
    <acc id="3"> acc3 </acc>
    <acc id="4"> acc4 </acc>
</test>
var iAccs=xdoc.substands(“test”).Elements(“acc”).Select(p=>p.Value);
List myList=新列表();
foreach(iAccs中的字符串p)
{
添加(p);
}

但是如何减去每个
元素的所有属性“id”值呢?

使用LINQ to XML可以很容易地得到:-

var iAccs = xdoc.Descendants("test").Elements("acc").Select(p => p.Value);
List<string> myList = new List<string>();
foreach(string p in iAccs)
{
    myList.Add(p);
}
XDocument xdoc=XDocument.Load(@“您的XML文件路径”);
列表结果=xdoc.子体(“acc”)
.Select(x=>(字符串)x.Attribute(“id”)).ToList();
或者,如果您更喜欢查询语法,则:-

XDocument xdoc = XDocument.Load(@"You XML file path");
List<string> result = xdoc.Descendants("acc")
                          .Select(x => (string)x.Attribute("id")).ToList();
List result2=(从xdoc.subjects(“acc”)中的x开始)
选择(int)x.Attribute(“id”).ToList();

使用LINQ to XML,您可以轻松获得:-

var iAccs = xdoc.Descendants("test").Elements("acc").Select(p => p.Value);
List<string> myList = new List<string>();
foreach(string p in iAccs)
{
    myList.Add(p);
}
XDocument xdoc=XDocument.Load(@“您的XML文件路径”);
列表结果=xdoc.子体(“acc”)
.Select(x=>(字符串)x.Attribute(“id”)).ToList();
或者,如果您更喜欢查询语法,则:-

XDocument xdoc = XDocument.Load(@"You XML file path");
List<string> result = xdoc.Descendants("acc")
                          .Select(x => (string)x.Attribute("id")).ToList();
List result2=(从xdoc.subjects(“acc”)中的x开始)
选择(int)x.Attribute(“id”).ToList();

您的意思是“提取”吗?要在一个查询中同时获取这两个元素:var iAccs=xdoc.substands(“test”).Elements(“acc”).Select(p=>new{value=(string)p,id=(int)p.Attribute(“id”).ToList();您的意思是“提取”吗?要在一个查询中同时获取这两个元素:var iAccs=xdoc.substands(“test”).Elements(“acc”).Select(p=>new{value=(string)p,id=(int)p.Attribute(“id”)).ToList();