[英]Get value in xml from c#
This question must be easy, but I faced a problem, which I can't deal with. 这个问题一定很容易,但是我遇到了一个我无法解决的问题。 No matter what I try I am unable to parse this xml with linq and get the xml value. 无论我尝试什么,我都无法使用linq解析此xml并获取xml值。
The error is "System.Collections.Generic.IEnumerable' does not contain a definition for 'Element' and no extension method 'Element' accepting a first argument of type 'System.Collections.Generic.IEnumerable' could be found (are you missing a using directive or an assembly reference?)" 错误为“ System.Collections.Generic.IEnumerable”不包含“元素”的定义,并且找不到扩展方法“元素”接受类型为“ System.Collections.Generic.IEnumerable”的第一个参数(您是否丢失了?使用指令或程序集引用?)”
I want to find the Xelement attribute.value which children have a concrete attribute.value. 我想找到Xelement attribute.value,它的孩子有一个具体的attribute.value。 How can I get the attribute.value? 如何获取attribute.value?
thanks 谢谢
xml XML文件
<submitInfo>
<setting name="file1" file ="example3.c" info ="open it!" serializeAs="String">
<add name="file11" program="example2.c" />
<add name="file12" value="example1.c" />
<value />
<setting name="file2" file ="example23.c" info ="open it!" serializeAs="String">
<add name="file21" program="example22.c" />
<add name="file22" value="example21.c" />
<value />
</setting>
</submitInfo>
code: 码:
var title1 = from q in doc.Element("content").Element("submitInfo").Elements("setting")
select q;
foreach (var t1 in title1)
{
Console.WriteLine(
String.Format(
name = title1.Element("name").Value,
file= title1.Element("file").Value,
info= title1.Attribute("info").Value));
}
//get setting info
var title = from p in doc.Element("content").Element("submitInfo").Element("setting").Elements("add")
select p;
foreach (var t1 in title)
{
Console.WriteLine(
String.Format(
name = title1.Element("name").Value,
value = title1.Element("program").Value));
This is one problem: 这是一个问题:
name = title1.Element("name").Value,
file= title1.Element("file").Value,
info= title1.Attribute("info").Value));
Look at your XML: 查看您的XML:
<setting name="file1" file ="example3.c" info ="open it!" serializeAs="String">
<add name="file11" program="example2.c" />
<add name="file12" value="example1.c" />
<value />
</setting>
It doesn't have a name
or file
element - those are attributes. 它没有name
或file
元素-它们是属性。 So you want something like: 所以你想要像这样的东西:
string name = t1.Attribute("name");
string file = t1.Attribute("file");
string info = t1.Attribute("info");
Note that this is using t1
, not title1
- otherwise you're asking for the data from the query, rather than for the specific element of the query. 请注意,这使用的是t1
,而不是title1
否则,您要从查询中获取数据,而不是查询中的特定元素。
Additionally, you really don't need a query expression here. 此外,您在这里确实不需要查询表达式。 Just use: 只需使用:
var title1 = doc.Element("content").Element("submitInfo").Elements("setting");
Another problem is that you're currently calling string.Format
with three assignments inside. 另一个问题是您当前正在调用string.Format
其中包含三个分配。 I suspect you actually wanted: 我怀疑您实际上想要的是:
Console.WriteLine("{0} {1} {2}", t1.Attribute("name"),
t1.Attribute("file"), t1.Attribute("info"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.