将 LINQ 嵌套到 XML
我有一些来自遗留应用程序的非标准 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
我认为你的问题是内部查询不是
List
,它可能是IEnumerable
或IQueryable
类型。 PODLink>
无法转换为List
。但是您可以执行ToList()
方法,它应该可以解决您的问题。更新:使代码看起来更好并删除了所有强制转换。
更新:修复了示例中的两个错误,因为我还没有编译它,所以可能还有更多错误。
Try this:
I think that your problem is that the inner query is not a
List<PODLink>
, it's probably of type ofIEnumerable<PODLink>
orIQueryable<PODLink>
which you can't cast toList<PODLink>
. But you can execute theToList()
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.
改为使用 XMLReader。代码多了一点,但效果很好。
Changed over to use an XMLReader. A bit more code but it works fine.