这是我从文件中获取xml值的代码,它确实成功但单个元素就像文件类型一样,但是当我尝试从xml获取记录数组时失败
public void readXmlFiles()
{
var xml = XDocument.Load(@"C:\Applications\Files\Xmldemo.xml");
var format = from r in xml.Descendants("insureance")
select new
{
type = r.Element("type").Value,
// values=r.Element("value").Value
};
foreach (var r in format)
{
Console.WriteLine(r.values);
}
}
这是我的xml文件
<?xml version="1.0" encoding="UTF-8"?>
<insureance>
<type>Risk</type>
<classofbusiness type="array">
<value>0</value>
<value>1</value>
<value>2</value>
</classofbusiness>
</insureance>
现在我想提前获取classofbusiness所有值,而不是提前
答案 0 :(得分:0)
您当前的尝试选择单个value
元素,该元素不是insureance
的子元素。我猜你会得到一个空引用异常。
您需要遵循文档的结构
var format =
from ins in doc.Descendants("insureance")
select new
{
Type = (string) ins.Element("type"),
ClassOfBusiness = ins.Elements("classofbusiness")
.Elements("value")
.Select(x => (int) x)
.ToArray()
};
答案 1 :(得分:0)
var format = xml.Elements("insureance").Elements("classofbusiness").Descendants();