robben07160

Hi,

If I have an XML in the format show below, and I want to get the PRODUCT_NUMBER, COLOUR, PRICE elements and then skip the order information and get the comment information (see code below). I can extract all data although can¡¯t seem to skip the ORDER tags to get the COMMENT data. I¡¯m using C++ and XmlTextReader. Is MoveToAttribute the wrong thing to use if I want to skip part of the tags in an XML file The error is gives me is get didn't expect the tag ORDER.

Thanks in advance for your help!

XmlTextReader *reader = new XmlTextReader(myProdFile);

while(reader->Read())

{

if (reader->MoveToContent() == XmlNodeType::Element && reader->Name->Equals("PRODUCTS"))

{

reader->Read();

reader->ReadElementString("PRODUCT_NUMBER");

reader->ReadElementString("COLOUR"));

reader->ReadElementString("PRICE");

reader->MoveToAttribute("COMMENT");

reader->ReadElementString("COMMENT");

}

else

{

reader->Read();

}

}

reader->Close();

< xml version="1.0" encoding="utf-8" >

<PRODUCTS>

<PRODUCT>

<PRODUCT_NUMBER>1</PRODUCT_NUMBER>

<COLOUR>Blue</COLOUR>

<PRICE>83.40</PRICE>

<ORDER>

<AMOUNT>1</AMOUNT>

<TOTAL>83.40</TOTAL>

</ORDER>

<ORDER>

<AMOUNT>2</AMOUNT>

<TOTAL>166.80</TOTAL>

</ORDER>

<COMMENT>Reorder</COMMENT>

</PRODUCT>

<PRODUCT>

<PRODUCT_NUMBER>2</PRODUCT_NUMBER>

<COLOUR>Yellow</COLOUR>

<PRICE>83.21</PRICE>

<ORDER>

<AMOUNT>2</AMOUNT>

<TOTAL>166.80</TOTAL>

</ORDER>

<COMMENT>Not in stock</COMMENT>

</PRODUCT>

</PRODUCTS>



Re: Visual C++ Language XML Skipping Nodes in XML (XmlTextReader)

robben07160

Would anyone know about skipping nodes

Thanks!





Re: Visual C++ Language XML Skipping Nodes in XML (XmlTextReader)

TilakGopi

Hi,

Use reader->ReadToNextSibling("COMMENT") instead reader->MoveToAttribute("COMMENT").as follows

---------------------------------------------------------------------------------------

XmlTextReader *reader = new XmlTextReader(myProdFile);

while(reader->Read())

{

if (reader->MoveToContent() == XmlNodeType::Element && reader->Name->Equals("PRODUCTS"))

{

reader->Read();

reader->ReadElementString("PRODUCT_NUMBER");

reader->ReadElementString("COLOUR"));

reader->ReadElementString("PRICE");

//reader->MoveToAttribute("COMMENT");

//Use this instead..

reader->ReadToNextSibling("COMMENT");

reader->ReadElementString("COMMENT");

}

else

{

reader->Read();

}

}

reader->Close();

------------------------------------------------------------------------------

Thanx,

Ch.T.Gopi Kumar.






Re: Visual C++ Language XML Skipping Nodes in XML (XmlTextReader)

robben07160

Hi Gopi,

Thanks for your reply, unfortunately I have .NET framework 1.1 and the function is only available in 2.0. The project must be developed using framework 1.1 therefore I can't use ReadToNextSibling.

Would there be an alternative to use which is compatible with 1.1

Thanks.





Re: Visual C++ Language XML Skipping Nodes in XML (XmlTextReader)

TilakGopi

Hi Robben,

Following is my sample working fine.Hope it has met ur requirement.

-------------------------------------------------------------

#include "stdafx.h"

#using <mscorlib.dll>

#using <System.Xml.dll>

using namespace System;

using namespace System::Xml;

bool MoveToElement(String *nodeName,XmlTextReader *pXmlReader)

{

while(true)

{

if(pXmlReader->LocalName->Equals(nodeName))

{

pXmlReader->Read();

return true;

}

if(!pXmlReader->Read()) break;

}

return false;

}

int _tmain()

{

// TODO: Please replace the sample code below with your own.

try

{

XmlTextReader *reader = new XmlTextReader("c:\\test.xml");

while(reader->Read())

{

if (reader->MoveToContent() == XmlNodeType::Element && reader->Name->Equals("PRODUCTS"))

{

String *str;

MoveToElement("PRODUCT_NUMBER",reader);

str = reader->get_Value();

MoveToElement("COLOUR",reader);

str= reader->get_Value();

MoveToElement("PRICE",reader);

str = reader->get_Value();

MoveToElement("COMMENT",reader);

str = reader->get_Value();

Console::WriteLine(str);

}

else

{

reader->Read();

}

}

reader->Close();

}

catch(Exception* ex)

{

Console::WriteLine(ex->Message);

}

return 0;

}

--------------------------------------------------------

Let me know , if u've any obscures in getting this.Hope this'll help u.

Thanx,

Ch.T.Gopi Kumar.






Re: Visual C++ Language XML Skipping Nodes in XML (XmlTextReader)

robben07160

Excellent Gopi many thanks for your hard work.

Kind regards!!