将 LINQ 嵌套到 XML

发布于 2024-10-11 19:26:26 字数 2391 浏览 1 评论 0原文

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

三寸金莲 2024-10-18 19:26:26

试试这个:

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,它可能是 IEnumerableIQueryable类型。 PODLink> 无法转换为 List。但是您可以执行 ToList() 方法,它应该可以解决您的问题。

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

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.

如梦 2024-10-18 19:26:26

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文