C++ CLI 中的 Linq

Linq in C++ CLI

我需要从 xml 文件中获取详细信息。我用 C# 编写代码但无法用 C++/CLI 重写。

C# 代码

 class cROI
{
    public Int16 iX { get; set; }
    public Int16 iY { get; set; }
    public Int16 iWidth { get; set; }
    public Int16 iHeight { get; set; }

    public cROI(Int16 iX, Int16 iY, Int16 iWidth, Int16 iHeight)
    {
        this.iX = iX;
        this.iY = iY;
        this.iWidth = iWidth;
        this.iHeight = iHeight;
        Console.WriteLine("{0}, {1}, {2}, {3}", this.iX, this.iY, this.iWidth, this.iHeight);
    }
}

主要

 List<cROI> m_lAllBoundingRectangle = new List<cROI>();

 XDocument xXmlDoc = XDocument.Load("C:/Users/Bradd/Desktop/abc.xml");

 var m_cROI = from rect in xXmlDoc.XPathSelectElements("/CLabelSessionContainer/Labels/LabelList/CLabel/VehicleLabel/BoundingRect")
                        select new cROI(
                            Int16.Parse(rect.Element("X").Value, System.Globalization.NumberFormatInfo.InvariantInfo),
                            Int16.Parse(rect.Element("Y").Value, System.Globalization.NumberFormatInfo.InvariantInfo),
                            Int16.Parse(rect.Element("Width").Value, System.Globalization.NumberFormatInfo.InvariantInfo),
                            Int16.Parse(rect.Element("Height").Value, System.Globalization.NumberFormatInfo.InvariantInfo)
                            );

m_lAllBoundingRectangle = m_cROI.ToList();

在 C++/CLI 中,我必须执行相同的操作,但我决定 Rectangle class.

而不是 4 个变量

我用C++/CLI写的代码如下,在Main()

Rectangle cBoudingRect;

List<Rectangle>^ m_AllBoudingRect = gcnew List<Rectangle>();

XDocument^ xXmlDoc = XDocument::Load("C:/Users/Bradd/Desktop/abc.xml");

auto getData = from rect in System::Xml::XPath::Extensions::XPathSelectElements
            ("/CLabelSessionContainer/Labels/LabelList/CLabel/VehicleLabel/BoundingRect");               ----> error 1: No suitable overloaded function available, asking for (node, string).

........ & unable to write further part as shown in C# code

因为System::Linq不可用,所以使用System::XMl::Linq & System::XMl::XPath。 "from, in, select" 不工作....

X,Y,Width, Height 在“./BoudingRect”节点下....

谢谢!!

编辑:XML的示例(注意:Tags/Nodes名称可能不同但结构相同)

<?xml version="1.0"?>

<CLabelContainer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <Labels>
    <LabelList>
      <CLabel>
        <VehicleLabel>
          <ImageName>image1.bmp</ImageName>
          <BoundingRect>
            <X>433</X>
            <Y>205</Y>
            <Width>39</Width>
            <Height>42</Height>
          </BoundingRect>
        </VehicleLabel>
      </CLabel>
      .
     & So on... 
      .
      <CLabel>
        <VehicleLabel>
          <ImageName>image20.bmp</ImageName>
          <BoundingRect>
            <X>425</X>
            <Y>305</Y>
            <Width>30</Width>
            <Height>46</Height>
          </BoundingRect>
        </VehicleLabel>
      </CLabel>
    </LabelList>
  </Labels>
</CLabelContainer>

C++/CLI既没有语言集成的查询语法,也没有扩展方法。

您可以做的是直接调用 System::Linq 函数作为静态函数:

List<int>^ list = Linq::Enumerable::ToList(Linq::Enumerable::Range(1, 20));

有一个好的开始here

我能够以这种方式重写您的代码:

using namespace System;
using namespace System::Collections::Generic;
using namespace System::Xml::Linq;
using namespace System::Xml::XPath;

ref class cROI
    {
    public:
        property int iX ;
        property int iY ;
        property int iWidth ;
        property int iHeight;

        cROI (int piX, int piY,int piWidth,int piHeight )
            {
            iX = piX;
            iY = piY;
            iWidth = piWidth;
            iHeight = piHeight;
            }
        virtual String^ ToString() override
            {
            return String::Format("{0}, {1}, {2}, {3}", iX, iY, iWidth, iHeight);
            }
    };


int main()
    {
    List<cROI^> ^m_lAllBoundingRectangle = gcnew List<cROI^>( );

    XDocument ^xXmlDoc = XDocument::Load( "E:/abc.xml" );

    IEnumerable<XElement^> ^query = xXmlDoc->Root->Descendants("BoundingRect");

    for each(XElement ^el in query)
        {
        m_lAllBoundingRectangle->Add(gcnew cROI(
            int(el->Element("X")),
            int(el->Element("Y")),
            int(el->Element("Width")),
            int(el->Element("Height"))));
        }
    for each(cROI ^el in m_lAllBoundingRectangle)
        {
        Console::WriteLine(el->ToString());
        }
    Console::ReadKey( true);
    return 0;
    }