问题
Can any one tell me how to parse XML which is in this format using XDocument in Windows phone 8
<search total="" totalpages="">
<domain>
<makes filter="">
<make cnt="374" image="abc.png">One</make>
<make cnt="588" image="bca">Two</make>
<make cnt="105" image="tley.png">Three</make>
<make cnt="458" image="mw.png">Four</make>
</makes>
</domain>
</search>
Right now i am using this code but unable to get the data out. I need image and name from this XML.
XDocument xdoc = XDocument.Parse(flickRes);
var rootCategory = xdoc.Root.Elements("makes");
List<string> list = new List<string>();
foreach (XElement book in rootCategory.Elements("make"))
{
string id = (string)book.Attribute("image");
string name = (string)book;
Debug.WriteLine(id);
//list.Add(data);
}
Thanks In Advance
回答1:
Elements
returns only direct children of current element (with matching name, when provided). Because <makes>
is not direct child of root element, xdoc.Root.Elements("makes")
will return empty collection.
Add another Element("domain")
call on xdoc.Root
before calling Element("makes")
.
XDocument xdoc = XDocument.Parse(flickRes);
var rootCategory = xdoc.Root.Element("domain").Element("makes");
List<string> list = new List<string>();
foreach (XElement book in rootCategory.Elements("make"))
{
string id = (string)book.Attribute("image");
string name = (string)book;
Debug.WriteLine(id);
//list.Add(data);
}
来源:https://stackoverflow.com/questions/23745665/parse-xml-using-xdocument-in-windows-phone-8