Xml Linq在C++;CLI

Xml Linq在C++;CLI,xml,linq,visual-c++,xpath,c++-cli,Xml,Linq,Visual C++,Xpath,C++ Cli,我需要从xml文件中获取详细信息。我用C#编写代码,但无法用C++/CLI重写 C代码# 大体上 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("/CLa

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

C代码#

大体上

 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();
由于System::Linq不可用,因此使用System::XMl::Linq和System::XMl::XPath。 “从,在,选择”不工作

十、 Y、宽度和高度在“/BoudingRect”节点下

谢谢

编辑:XML示例(注意:标记/节点名称可能不同,但结构相同)


image1.bmp
433
205
39
42
.
&等等。。。
.
image20.bmp
425
305
30
46

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

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

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

这是一个良好的开端。

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

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

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

这是一个良好的开端。

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

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;
    }
使用名称空间系统;
使用命名空间System::Collections::Generic;
使用名称空间System::Xml::Linq;
使用名称空间System::Xml::XPath;
参考类cROI
{
公众:
财产int iX;
财产保险;
iWidth的财产;
财产所有权;
cROI(整数像素、整数像素、整数像素宽度、整数像素高度)
{
iX=piX;
iY=piY;
iWidth=像素宽度;
iHeight=高度;
}
虚拟字符串^ToString()重写
{
返回字符串::Format(“{0},{1},{2},{3}”,iX,iY,iWidth,iHeight);
}
};
int main()
{
List^m_lAllBoundingRectangle=gcnewlist();
XDocument^xXmlDoc=XDocument::Load(“E:/abc.xml”);
IEnumerable^query=xXmlDoc->Root->substands(“BoundingRect”);
对于每个(查询中的元素^el)
{
m_lAllBoundingRectangle->添加(新cROI)(
int(el->Element(“X”),
int(el->Element(“Y”),
int(el->Element(“宽度”),
int(el->Element(“高度”);
}
对于每个(m_lAllBoundingRectangle中的cROI^el)
{
控制台::WriteLine(el->ToString());
}
控制台::ReadKey(true);
返回0;
}

我可以用以下方式重写您的代码:

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;
    }
使用名称空间系统;
使用命名空间System::Collections::Generic;
使用名称空间System::Xml::Linq;
使用名称空间System::Xml::XPath;
参考类cROI
{
公众:
财产int iX;
财产保险;
iWidth的财产;
财产所有权;
cROI(整数像素、整数像素、整数像素宽度、整数像素高度)
{
iX=piX;
iY=piY;
iWidth=像素宽度;
iHeight=高度;
}
虚拟字符串^ToString()重写
{
返回字符串::Format(“{0},{1},{2},{3}”,iX,iY,iWidth,iHeight);
}
};
int main()
{
List^m_lAllBoundingRectangle=gcnewlist();
XDocument^xXmlDoc=XDocument::Load(“E:/abc.xml”);
IEnumerable^query=xXmlDoc->Root->substands(“BoundingRect”);
对于每个(查询中的元素^el)
{
m_lAllBoundingRectangle->添加(新cROI)(
int(el->Element(“X”),
int(el->Element(“Y”),
int(el->Element(“宽度”),
int(el->Element(“高度”);
}
对于每个(m_lAllBoundingRectangle中的cROI^el)
{
控制台::WriteLine(el->ToString());
}
控制台::ReadKey(true);
返回0;
}

我已经读过了,但我真的很困惑如何使用它来编写我的4行代码???@user3042916首先编写一个函数,如
cROI^MakeACRoi(XElement^element)
。。。那么关于如何使用这个函数,我已经读过了,但是我真的很困惑如何使用这个函数来编写我的4行代码???@user3042916首先编写一个函数,比如
cROI^MakeACRoi(XElement^element)
。。。那么对于如何使用这个函数,看看它在C++/CLI中是一件很麻烦的事情,缺乏对lambda表达式、扩展方法、匿名类型和查询理解的支持是相当痛苦的。不用麻烦,利用.NET中优秀的语言互操作性。您可以在C#类库中编写此代码,并直接从C++/CLI代码中调用它。@user3042916,您能给出一个xml示例吗?@HansPassant-听起来不错。我需要看一次。由于我是初学者,如果你找到好的资源来做这件事,请与我分享。我不知道如何创建程序集或类库(我知道类,但类库:()。谢谢。在C++/CLI中,这是一件很麻烦的事情,缺少对lambda表达式、扩展方法、匿名类型和查询理解的支持是相当痛苦的。不用麻烦,利用.NET中优秀的语言互操作性。您可以在C++类库中编写此代码,并直接从C++/CLI代码中调用。@user3042916,你能举一个xml的例子吗?@HansPassant-听起来不错。我需要看一次。因为我是初学者,如果你找到很好的资源来做这件事,请与我分享。我不知道如何创建程序集或类库(我知道类,但类库:()。谢谢。
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;
    }