将LINQ嵌套到XML

将LINQ嵌套到XML,xml,linq,linq-to-xml,Xml,Linq,Linq To Xml,我从一个旧应用程序中获得了一些非标准XML: <PODRoot> <RowData> <PODItem>Item243</PODItem> <StoragePath>PODItem66</StoragePath> <ID>-13</ID> <PODType>3</PODType> <Description>Transfer to MPF PODs</Descr

我从一个旧应用程序中获得了一些非标准XML:

<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>

项目243
裙带关系66
-13
3.
转强积金站
1.
HTMLPage1.htm
HTMLPage1.htm
2.
HTMLPage2.htm
HTMLPage2.htm
...

并尝试在SL4应用程序中执行以下操作,但对PODLink节点无效:

            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

                       //                        };
                   };
List Nodes=(List)来自doc.substands(“RowData”)中的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=(列表)来自podNode.substands(“podLink”)中的podLink
//选择新的播客链接
//                        {
//LinkType=podLink.Element(“LinkType”).Value,
//位置=podLink.Element(“位置”).Value,
//Description=podLink.Element(“Description”).Value
//                        };
};
以下是相关课程:

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;
}
公共类PODNode
{
公共节点类型PODNodeType;
公共字符串ItemName=string.Empty;
公共int ID;
publicstringstoragepath=string.Empty;
公共字符串描述=string.Empty;
publicstringcomment=string.Empty;
公共列表节点;
公共列表链接;
}
你知道我怎样才能让被注释掉的部分正常工作吗?

试试这个:

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();
List Nodes=(来自doc.substands(“RowData”)中的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=(来自podNode.substands(“podLink”)中的podLink)
选择新的播客链接
{
LinkType=podLink.Element(“LinkType”).Value,
位置=podLink.Element(“位置”).Value,
Description=podLink.Element(“Description”).Value
})托利斯先生()
}).ToList();
我认为您的问题在于内部查询不是
列表
,它可能是
IEnumerable
IQueryable
类型,您无法将其转换为
列表
。但是您可以执行
ToList()
方法,它应该可以解决您的问题

更新:使代码看起来更漂亮,并删除了所有强制转换。
更新:修复了示例中的两个错误,可能还有更多错误,因为我没有编译它。

改为使用XMLReader。代码多一点,但效果很好。

该代码会出现大量错误,其中大部分是无效表达式。谢谢你的回复。但是如果你说你的错误是什么,那么帮助你就容易多了。另外,我没有编译代码,我只是指出了一部分是错误的。我想一张图片会比把它全部压缩在这里要好——你自己也试过解决它吗?Visual studio基本上会告诉您问题出在哪里,从第一个错误开始,即
应该在
from
中的
之前,而不是
select
…正如我所说,我没有编译代码,只是直接从头开始编写。我将在示例中修复第一个错误。