如何筛选 XmlDocument、xmlDocument.LoadXml 和 xmlDocument.SelectNodes 中的行(类似于 where 子句)

How to filter rows in XmlDocument, xmlDocument.LoadXml and xmlDocument.SelectNodes (similar to a where clause)

提问人:StackTrace 提问时间:11/18/2022 最后编辑:StackTrace 更新时间:11/18/2022 访问量:39

问:

我有一个 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。MyOtherListMyOtherList.Add(new OtherList { Id = x.Id, Region = new Guid(node.InnerText) });

C# XMLDocument

评论

0赞 Bagus Tesa 11/18/2022
可以肯定的是,您希望 SQL 查询在决定是否加载内容 XML 列之前查看它们?最愚蠢的方法是将其视为字符串或使用原始查询。Entity Framework 删除了 XML/JSON 列集成的许多功能请求,#19049#6284。或者,使用 XPath 或其他东西(如 LINQ 的)在应用程序端执行繁重的工作。Where
0赞 StackTrace 11/18/2022
@BagusTesa 我想做的是,但我正在努力将“linq's Where”之类的东西应用于该代码。关于我如何实现这一目标的任何线索?

答: 暂无答案