提问人:StackTrace 提问时间:11/18/2022 最后编辑:StackTrace 更新时间:11/18/2022 访问量:39
如何筛选 XmlDocument、xmlDocument.LoadXml 和 xmlDocument.SelectNodes 中的行(类似于 where 子句)
How to filter rows in XmlDocument, xmlDocument.LoadXml and xmlDocument.SelectNodes (similar to a where clause)
问:
我有一个 SQL Server 表,其中包含一个 XML 列 (xmlColumn1),该列可以包含如下所示的 xml 数据。
<!--Row 1--column (xmlColumn1)
<parameters>
<Category>Business</Category>
<Region>CCF25F36-B2A7-4790-82A4-7EC5E016C197</Region>
<CountryId>EC7333E0-7BEE-4913-9973-2FCABE3DA6F0</CountryId>
</parameters>
<!--Row 2--column (xmlColumn1)
<parameters>
<Category>Politics</Category>
<Region>CCF25F36-B2A7-4790-82A4-7EC5E016C197</Region>
<CountryId>EC7333E0-7BEE-4913-9973-2FCABE3DA6F0</CountryId>
</parameters>
<!--Row 3--column (xmlColumn1)
<parameters>
<Category>Business</Category>
<Region>CCF25F36-B2A7-4790-82A4-7EC5E016C197</Region>
<CountryId>EC7333E0-7BEE-4913-9973-2FCABE3DA6F0</CountryId>
</parameters>
<!--Row 4--column (xmlColumn1)
<parameters>
<Category>Sports</Category>
<Region>CCF25F36-B2A7-4790-82A4-7EC5E016C197</Region>
<CountryId>EC7333E0-7BEE-4913-9973-2FCABE3DA6F0</CountryId>
</parameters>
然后,我有一个类似于下面的SQL选择查询:
SELECT Id, xmlColumn1
FROM MyTable
上述查询返回的行将加载到如下所示的列表中。
List<myData> Data = MyDataSource.GetMyData();
我现在处理这些数据并将其添加到另一个列表中,如下所示。
if (Data.Count.Count > 0)
{
(Data.ForEach(x =>
{
var xmlDocument = new XmlDocument();
xmlDocument.LoadXml(x.id);
XmlNodeList paramsList = xmlDocument.SelectNodes("//Region");
foreach (XmlNode node in paramsList)
{
MyOtherList.Add(new OtherList { Id = x.Id, Region = new Guid(node.InnerText) });
}
});
我怎样才能在上面的代码中添加一个条件,以便仅将XML列参数为“Business”的行添加到。"Category"
MyOtherList
如果参数类别不是 Buiness,我不想将行添加到 at。MyOtherList
MyOtherList.Add(new OtherList { Id = x.Id, Region = new Guid(node.InnerText) });
答: 暂无答案
评论
Where