开发者

Nesting LINQ to XML

I have some non-standard XML from a legacy app:

<PODRoot>
<RowData>
<PODItem>Item243</PODItem>
<StoragePath>PODItem66</StoragePath>
<ID>-13</ID>
<开发者_开发问答PODType>3</PODType>
<Description>Transfer to MPF PODs</Description>
<MemoString></MemoString>
<PODLink>
  <LinkType>1</LinkType>
  <Location>HTMLPage1.htm</Location>
  <Description>HTMLPage1.htm</Description>
</PODLink>
<PODLink>
  <LinkType>2</LinkType>
  <Location>HTMLPage2.htm</Location>
  <Description>HTMLPage2.htm</Description>
</PODLink>

...

and tried to do the following in an SL4 app, which didn't work for the PODLink nodes:

            List<PODNode> Nodes = (List<PODNode>)from podNode in doc.Descendants("RowData")
                   select new PODNode
                   {
                       ItemName = podNode.Element("PODItem").Value,
                       StoragePath = podNode.Element("StoragePath").Value,
                       ID = Convert.ToInt32(podNode.Element("ID").Value),
                       PODNodeType = (NodeType)Convert.ToInt32(podNode.Element("PODType").Value),
                       Description = podNode.Element("Description").Value,
                       Comment = podNode.Element("MemoString").Value,
                       //Links = (List<PODLink>)from podLink in podNode.Descendants("PODLink")
                       //                        select new PODLink
                       //                        {
                       //                            LinkType = podLink.Element("LinkType").Value,
                       //                            Location = podLink.Element("Location").Value,
                       //                            Description = podLink.Element("Description").Value

                       //                        };
                   };

Here's the relevant class:

public class PODNode
{
    public NodeType PODNodeType;
    public string ItemName = string.Empty;
    public int ID;
    public string StoragePath = string.Empty;
    public string Description = string.Empty;
    public string Comment = string.Empty;
    public List<PODNode> Nodes;
    public List<PODLink> Links;
}

Any idea how, is it's possible, I can get the commented out portion working?


Try this:

List<PODNode> Nodes = (from podNode in doc.Descendants("RowData")
       select new PODNode
       {
           ItemName = podNode.Element("PODItem").Value,
           StoragePath = podNode.Element("StoragePath").Value,
           ID = Convert.ToInt32(podNode.Element("ID").Value),
           PODNodeType = (NodeType)Convert.ToInt32(podNode.Element("PODType").Value),
           Description = podNode.Element("Description").Value,
           Comment = podNode.Element("MemoString").Value,
           Links = (from podLink in podNode.Descendants("PODLink")
               select new PODLink
               {
                   LinkType = podLink.Element("LinkType").Value,
                   Location = podLink.Element("Location").Value,
                   Description = podLink.Element("Description").Value

               }).ToList()
       }).ToList();

I think that your problem is that the inner query is not a List<PODLink>, it's probably of type of IEnumerable<PODLink> or IQueryable<PODLink> which you can't cast to List<PODLink>. But you can execute the ToList() method and it should solve your problem.

UPDATE: Made code look nicer and removed all casts. UPDATE: Fixed two errors in the example, there might be more since I haven't compiled it.


Changed over to use an XMLReader. A bit more code but it works fine.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜