将 LINQ 嵌套到 XML

2024-05-19

我有一些来自遗留应用程序的非标准 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>

...

并尝试在 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

                       //                        };
                   };

这是相关的类:

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

知道如何,是否有可能,我可以让注释掉的部分正常工作?


尝试这个:

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<PODLink>,它可能属于以下类型IEnumerable<PODLink> or IQueryable<PODLink>你无法投射到的List<PODLink>。但你可以执行ToList()方法,它应该可以解决你的问题。

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将 LINQ 嵌套到 XML 的相关文章

随机推荐