473,385 Members | 1,901 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Noobish C# question about XML parsing...

WTH
In C++ I had, long ago, written my own XML parsing class as I never found
even a half decent node based hierarchical solution that was simple, now
that I'm starting to develop tools in C# (as a precursor to moving our NT
services, COM/DCOM, and client apps into C#) I'm wondering if there's
something like this out there.

For example, to use my CXMLNode class you could just do this:

CXMLNode l_oRoot;

if( false == l_oRoot.Parse( in_strXML ) )
{
MessageBox( NULL, _T("You gave me crappy XML"), _T("XML be ungood"),
MB_OK );
return false;
}

//How many child nodes in this XML structure?
long l_nChildren = l_oRoot.GetNumberOfChildren();

//Get first node we have (the node used to start parsing XML is just a root
node/container for the 'real' XML nodes)
CXMLNode* l_pNode = l_oRoot.GetChild(0);

//Get an attribute off of this node
tstring l_strMyAttribute = l_pNode->GetAttribute( "MyAttribute" );

//Get a text value off this node if it has one
tstring l_strValue = l_pNode->GetTextValue();

//Find a child node from this node named MyHardToFindNode
CXMLNode* l_pNode2 = l_pNode->FindChildNode( "MyHardToFindNode" );

Et cetera, ad nauseum...

Is there something like this already in C# or am I heading for
"porting-ville"?

WTH

--
"I want to keep an English heart to the team. I believe in that. Michael
Owen is that. Never think Michael is afraid of anything." - Gérard Houllier
Jul 30 '06 #1
5 2388
Hi WTH,

..Net has extensive support for XML in all kinds of ways, like XmlDocument, XmlNode, XmlAttribute and XPath queries.
On Sun, 30 Jul 2006 17:26:10 +0200, WTH <sp*******@Ih8it.comwrote:
In C++ I had, long ago, written my own XML parsing class as I never found
even a half decent node based hierarchical solution that was simple, now
that I'm starting to develop tools in C# (as a precursor to moving ourNT
services, COM/DCOM, and client apps into C#) I'm wondering if there's
something like this out there.

For example, to use my CXMLNode class you could just do this:

CXMLNode l_oRoot;

if( false == l_oRoot.Parse( in_strXML ) )
{
MessageBox( NULL, _T("You gave me crappy XML"), _T("XML be ungood"),
MB_OK );
return false;
}

//How many child nodes in this XML structure?
long l_nChildren = l_oRoot.GetNumberOfChildren();

//Get first node we have (the node used to start parsing XML is just aroot
node/container for the 'real' XML nodes)
CXMLNode* l_pNode = l_oRoot.GetChild(0);

//Get an attribute off of this node
tstring l_strMyAttribute = l_pNode->GetAttribute( "MyAttribute" );

//Get a text value off this node if it has one
tstring l_strValue = l_pNode->GetTextValue();

//Find a child node from this node named MyHardToFindNode
CXMLNode* l_pNode2 = l_pNode->FindChildNode( "MyHardToFindNode" );

Et cetera, ad nauseum...

Is there something like this already in C# or am I heading for
"porting-ville"?

WTH


--
Happy coding!
Morten Wennevik [C# MVP]
Jul 30 '06 #2
WTH
Morten Wennevik <Mo************@hotmail.comloquated like no one had ever
loquated before with:
Hi WTH,

.Net has extensive support for XML in all kinds of ways, like
XmlDocument, XmlNode, XmlAttribute and XPath queries.
I was sure it did, but wanted to know if something like this:

XmlDocument doc = new XmlDocument();
doc.Load("sample.xml");
XmlNodeReader reader = new XmlNodeReader(doc);

Was what I was looking for or not...

Thanks,
--
"I'm very demanding in terms of work and spirit. I think I'm a very
nice man but I can be a very nasty man if someone is not behaving
right. If someone upsets the harmony of what we are trying to do, I am
10 times nastier than anyone else." - Gérard Houllier
Jul 30 '06 #3
I should add that your samples would translate to something like:

XmlDocument doc = new XmlDocument();

try
{
doc.LoadXml(in_strXML);
}
catch
{
MessageBox.Show("You gave me crappy XML", "XML be ungood");
return false;
}

XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);

//load namespaces

string query = "//*";

XmlNodeList nodeList = doc.DocumentElement.SelectNodes(query, nsmgr);

int numberOfChildren = nodeList.Count;

XmlNode firstNode = doc.ElementNode.FirstChild;

XmlAttribute attribute = firstNode.Attributes["MyAttribute"];

string value = attribute.Value;

string query = "//MyHardToFindNode";

XmlNode hardToFindNode = doc.ElementNode.SelectSingleNode(query);
The code is untested and the xpath queries most likely not correct.
--
Happy coding!
Morten Wennevik [C# MVP]
Jul 30 '06 #4
I partly answered this in a reply to my own post. I can't claim to havedone much work with XML, but so far I have not used XmlNodeReader.

On Sun, 30 Jul 2006 18:06:47 +0200, WTH <sp*******@Ih8it.comwrote:
Morten Wennevik <Mo************@hotmail.comloquated like no one had ever
loquated before with:
>Hi WTH,

.Net has extensive support for XML in all kinds of ways, like
XmlDocument, XmlNode, XmlAttribute and XPath queries.

I was sure it did, but wanted to know if something like this:

XmlDocument doc = new XmlDocument();
doc.Load("sample.xml");
XmlNodeReader reader = new XmlNodeReader(doc);

Was what I was looking for or not...

Thanks,


--
Happy coding!
Morten Wennevik [C# MVP]
Jul 30 '06 #5
WTH
Morten Wennevik <Mo************@hotmail.comloquated like no one had ever
loquated before with:

<snip>

Thanks Morten :).

WTH
--
"I've only one medal left to win at Liverpool and that's the
Premiership. That's what I want more than anything and Liverpool is the
only place I've ever wanted to win it." - Steven Gerrard
Jul 30 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Gerrit Holl | last post by:
Posted with permission from the author. I have some comments on this PEP, see the (coming) followup to this message. PEP: 321 Title: Date/Time Parsing and Formatting Version: $Revision: 1.3 $...
16
by: Terry | last post by:
Hi, This is a newbie's question. I want to preload 4 images and only when all 4 images has been loaded into browser's cache, I want to start a slideshow() function. If images are not completed...
2
by: Michael Thomas | last post by:
Hi all Just out of curiosity, what is the difference between VB and VBA and how does this impact on Microsoft Access? Thanks, Michael
1
by: Gidi | last post by:
hi, how can i check that the input that the user inserts is of the type i need. for expample if i want the user to insert only numbers how can i check that he hasn't insert letters or something...
10
by: sp | last post by:
I create an xml file in a text editor: <?xml version="1.0" encoding="utf-8"?> <elts> <elt id="1" class="c1">content1</elt> <elt id="2" class="c1">content2</elt> </elts> Then I load the file...
10
by: Scott | last post by:
Ok, I've lost half a day to pounding my head against this one and I'm sending out a distress call... This is so simple that it's emberassing. However, VC# 2005 Express has made things so...
2
by: shrynn | last post by:
Hey guys, This is a noobish question I guess but here goes: I am attempting to recalc some of our Payroll data from Detail. I am summing the total dollar figure from each deduction code for the...
6
by: whiteideal | last post by:
hay, guys, i just want to create a svg file according to my data, but i don't know whether there is some opensouce lib to do such things. Can you tell me about it? Thanks.
1
by: eyeore | last post by:
Hello everyone my String reverse code works but my professor wants me to use pop top push or Stack code and parsing code could you please teach me how to make this code work with pop top push or...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.