0

I need to get details from a xml file. I wrote code in C# but not able to rewrite in C++/CLI.

Code in 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);
    }
}

In main

 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();

In C++/CLI i have to do same operation but instead of 4 variables, i decided to Rectangle class.

Code i wrote in C++/CLI is as follows, in 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

as System::Linq is not available, System::XMl::Linq & System::XMl::XPath is used. "from, in, select" are not working....

X,Y,Width, Height are under "./BoudingRect" node....

Thank you !!

Edit : Example for an XML ( Note : Tags/Nodes name may be different but structure is same)

<?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>
user3042916
  • 87
  • 3
  • 12
  • 1
    It is a plodding affair in C++/CLI, the lack of support for lambda expressions, extension methods, anonymous types and query comprehensions is quite painful. Just don't bother, take advantage of the excellent language interop in .NET. You can write this code in a C# class library and call it directly from your C++/CLI code. – Hans Passant Jun 18 '15 at 13:22
  • @user3042916, can you give an example of xml? –  Jun 18 '15 at 16:55
  • @HansPassant - That's sounds great. I need to look at it once. As i am beginner, if you find good resources to do this, please share with me. I don't how to create assembly or class library ( class i know but class library :( ) .Thanks. – user3042916 Jun 18 '15 at 17:32

2 Answers2

3

C++/CLI has neither the language integrated query syntax, nor extension methods.

What you could do is calling the System::Linq functions directly as static functions:

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

There is a good start here.

Community
  • 1
  • 1
nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • I read it already, but i am really confused how to use this to write my 4 line of code ?? – user3042916 Jun 18 '15 at 12:59
  • 1
    @user3042916 Begin by writing a function like `cROI^ MakeACRoi(XElement^ element)`... Then for how to use this function look at http://stackoverflow.com/a/5645267/613130 – xanatos Jun 18 '15 at 13:03
1

I was able to rewrite your code in such a way:

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;
    }