Noobish C# question about XML parsing...

W

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
 
M

Morten Wennevik

Hi WTH,

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

WTH

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,
 
M

Morten Wennevik

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.
 
M

Morten Wennevik

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top