Notifications
Clear all

[Closed] .net xml reader class

Does anyone know if there is a .net xml reader class that is not forward-only? I need to search an .xml file for a certain attribute, and if that attribute is found move to the parent element in the xml file. Any hints or help would be great.

Thanks,
Jon

7 Replies
1 Reply
(@rustyknight)
Joined: 11 months ago

Posts: 0

I’m pretty sure that any DOM based XML parser has to read the entire document into “memory” before you can manipulate it anyway.

This would suggest that you now have the ability to determine how you track over the document…

So basically, I’d suggest, you need to get the node count and then work you way backwards…??

Without knowing exactly what you are trying (ie what you are looking for – node attribute, node value, node parent/child…etc…) It is somewhat difficult to provide any kind of example.

The DOM based parser should provide you with the ability to traverse a document in any direction from any point…

Shane

Well, more specifically I have a .xml file that contains a lot of different things. Some of the elements are mesh names with positions and rotations. Something like this:

<teapot=“object” >
<localtransform>
<pos x=“54.2726” y=”-97.1453″ z=“270.0” />
<quat x=”-1.19209e-007″ y=“0.0” z=“0.0” w=“1.0”/>
</localtransform>
<mesh rsrc=“teapot01” />
</teapot>

However, the localtransform and the mesh positions can be reversed. So I need to find the “mesh” instance and traverse back to the parent element and look for localtransform. I’m a bit confused on how I could do that. To verify, the steps would be, search for mesh, traverse back to teapot, then search for localtransform.

Thanks,
Jon

*edit – sorry the xml didn’t format appropriately

This is a bit of simplified method and I can probably think of a number of different ways of approaching it, but

		-- I'm jumping here...This is a list of all "teapot" nodes...
		local nodeList = xmlDoc.getElementsByTagName "teapot"
		for iIndex = 0 to (nodeList.count - 1) do (
			local node = (nodeList.item iIndex)
			
			if (stricmp node.name "Mesh") == 0 then (
				local parentNode = node.parent
				-- Reprocess details...??
			)
		)

Will do what you are asking…

I would personally run over the whole node, collect all the info I need, regardless of order and then work out the whole mess when I’m done.

While I was investigating the code for this, I run across the method “XmlNode.SelectSingleNode” which uses the xml path syntex (XPath) which would probably make a huge difference to how you would manage this. It would be worth a read…if I had time

Shane

 PEN

Yes there is a .net class that will make this all very easy for you. I’m not at my machine but I believe this is what I’m using here. http://msdn2.microsoft.com/en-us/library/system.xml.aspx

I have only had to write a hand full of functions to make it a bit easier on doing the searching like a function that will return an element if certain parameters or there values exist.

Have a look and I will check in later when I’m on my machine and can give you mode help if needed without looking it all up.

Thanks Guys. Yeah Pen, I’ve been looking into the .net xml documentation, specifically the xmlReader class. However, I haven’t found a method to do exactly what I’m after. I’m trying to look for a certain node (“mesh”) and search it’s siblings within that element above and below for “localtransform”. I can use xmlReader.ReadToNextSibling() to find the elements after “mesh”, but haven’t found a way to traverse back up the tree. In psuedo code what I’m looking for is something like this


 while XmlReader.read() do
 (
 	if XmlReader.name == "mesh" do
 	(
 		obj = XmlReader.getAttribute "rsrc"
 		XmlReader.MoveToParentNode() --this is the function I'm searching for
 		XmlReader.ReadToDescendent "localtransform"
 		XmlReader.ReadToDescendent "pos" --etc.
 	)
 )

Perhaps I’m going about this the wrong way though. Again, thanks for your help.

–Jon

If you have .NET Framework 3.5 (Beta), you could use Linq to Xml.

Light

Thanks for the help guys. I figured out a suitable solution. I’m searching for the attribute class and based on the string that is returned using xmlReader’s ReadSubTree method to scan that chunk of code and look for the mesh information and transform.

–Jon