本文介绍了在C#中解析XML文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
朋友们,
i需要解析xml文档并将值存储在不同的变量或数组中。
这里我的xml文档
hi friends,
i need to parse xml document and store values in different variables or array.
here my xml document
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetReviewsFromManufacturerCategoryAndTypeResponse xmlns="http://www.caranddriving.com/ws">
<GetReviewsFromManufacturerCategoryAndTypeResult>
<code>0</code>
<message />
<count>8</count>
<reviewlist>
<review>
<reviewid>309171</reviewid>
<manufacturer>Audi</manufacturer>
<nameofcar>Audi A1</nameofcar>
<paragraphlist>
<paragraph>
<type>By Line</type>
<headline />
<text>Audi's A1 might well be at its best when fitted with a particularly clever 1.4-litre petrol engine. Andy Enright reports.</text>
</paragraph>
<paragraph>
<type>Ten Second Review</type>
<headline>Ten Second Review</headline>
<text>The A1 1.4 TFSI CoD features a modern turbocharged petrol engine that features a cylinder cut-off system which means that when cruising, it'll run on two rather than four cylinders. The result? Economy of 60.1mpg, yet it has the ability to nip to 60mph in less than 8 seconds.</text>
</paragraph>
</paragraphlist>
<photolist>
<photo>
<mini>http://f2.caranddriving.com/images/new/mini/AudiA114TFSICoD0313(2).jpg</mini>
<std>http://f2.caranddriving.com/images/new/std/AudiA114TFSICoD0313(2).jpg</std>
<big>http://f2.caranddriving.com/images/new/big/audia114tfsicod0313(2).jpg</big>
<large>http://f2.caranddriving.com/images/new/large/AudiA114TFSICoD0313(2).jpg</large>
<isdefault>false</isdefault>
</photo>
<photo>
<mini>http://f2.caranddriving.com/images/new/mini/AudiA114TFSICoD0313.jpg</mini>
<std>http://f2.caranddriving.com/images/new/std/AudiA114TFSICoD0313.jpg</std>
<big>http://f2.caranddriving.com/images/new/big/audia114tfsicod0313.jpg</big>
<large>http://f2.caranddriving.com/images/new/large/AudiA114TFSICoD0313.jpg</large>
<isdefault>true</isdefault>
</photo>
</photolist>
</review>
<review>
<reviewid>8985</reviewid>
<manufacturer>Audi</manufacturer>
<nameofcar>Audi A1 1.6 TDI SE</nameofcar>
<paragraphlist>
<paragraph>
<type>By Line</type>
<headline />
<text>Audi's A1 might well be at its best when fitted with a particularly clever 1.4-litre petrol engine. Andy Enright reports.</text>
</paragraph>
<paragraph>
<type>Ten Second Review</type>
<headline>Ten Second Review</headline>
<text>The A1 1.4 TFSI CoD features a modern turbocharged petrol engine that features a cylinder cut-off system which means that when cruising, it'll run on two rather than four cylinders. The result? Economy of 60.1mpg, yet it has the ability to nip to 60mph in less than 8 seconds.</text>
</paragraph>
</paragraphlist>
<photolist>
<photo>
<mini>http://f2.caranddriving.com/images/new/mini/AudiA114TFSICoD0313(2).jpg</mini>
<std>http://f2.caranddriving.com/images/new/std/AudiA114TFSICoD0313(2).jpg</std>
<big>http://f2.caranddriving.com/images/new/big/audia114tfsicod0313(2).jpg</big>
<large>http://f2.caranddriving.com/images/new/large/AudiA114TFSICoD0313(2).jpg</large>
<isdefault>false</isdefault>
</photo>
<photo>
<mini>http://f2.caranddriving.com/images/new/mini/AudiA114TFSICoD0313.jpg</mini>
<std>http://f2.caranddriving.com/images/new/std/AudiA114TFSICoD0313.jpg</std>
<big>http://f2.caranddriving.com/images/new/big/audia114tfsicod0313.jpg</big>
<large>http://f2.caranddriving.com/images/new/large/AudiA114TFSICoD0313.jpg</large>
<isdefault>true</isdefault>
</photo>
</photolist>
</review>
</GetReviewsFromManufacturerCategoryAndTypeResult>
</GetReviewsFromManufacturerCategoryAndTypeResponse>
</soap:Body>
</soap:Envelope>
来自这个xml文档,它有两个评论列表。
但是我需要找到哪个评论的车名为Audi A1并提取该特定节点的数据(制造商,照片,段落)。
任何人帮我写xml解析器。
问候,
Lalitha
from this xml document,it have two review list.
but i need to find that which review has car name as "Audi A1" and extract datas(manufacturer,photo,paragraph) of that particular node.
any one help me write the xml parser.
Regards,
Lalitha
推荐答案
XDocument xDocument = XDocument.Parse(xml);
string manufacturer = string.Empty;
var aa = xDocument.Descendants("review").Where(x => x.Element("nameofcar").Value.Equals("Audi A1"));
foreach (var a in aa)
{
manufacturer = a.Element("manufacturer").Value;
}
这篇关于在C#中解析XML文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!