2

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?

0

2 Answers 2

3

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.

4
  • Get a bunch of errors with that code, invalid expressions mostly. Thanks for the response though.
    – Jim Perry
    Commented Jan 12, 2011 at 16:14
  • But if you say what your error is it would be much easier to help you. Also, I didn't compile the code, I just pointed out one part that was wrong. Commented Jan 12, 2011 at 16:30
  • I guess a picture would be better than trying to squeeze it all in here - flickr.com/photos/25773555@N02/5349040439
    – Jim Perry
    Commented Jan 12, 2011 at 17:16
  • Have you even tried to solve it yourself? Visual studio basically tells you what is wrong, start with the first error, which is that the ( should be before the from instead of select... as I said, I didn't compile the code, just wrote it directly from my head. I'll fix the first error in the example. Commented Jan 12, 2011 at 20:09
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.