all groups > dotnet xml > january 2005 >
You're in the

dotnet xml

group:

newbie question on finding and editing


newbie question on finding and editing GeorgeAtkins
1/30/2005 9:39:03 AM
dotnet xml: Using vb.net, I need to search through XML files to locate specific elements
and once found, update attributes of their companion elements. In plain
language, I need to find specific book titles and update note information
about each book. Not every book would be updated; just selected ones.

So, I am confused whether to use DOM, XPath, both, or something else? My
understandig of Xpathnavigator is that it is a read-only class. And DOM seems
good for mass updates, but how to use it to find things? Thanks for any
advise!

RE: newbie question on finding and editing GeorgeAtkins
1/31/2005 2:05:01 PM
Thanks for the refs and examples! I actually have waded through these before;
I was confused by apparent differences between capabilities of DOM and XPATH
and other searching methods. Which is to say, I was over my head. I also
appreciate the sample code, which is easy to understand, by the way.

My need is not so straightforward as iterating and changing every occurrence
of an element. Rather, I have to search for specific book titles within the
collection and update a related quiz field for that book. In XML terms, I
think, I need to find specific attribute text for a given element, then find
a sibling element and update its attribute text.

So, I'll re-read those listings, but I may go out and find a good book that
offers more contextual help and examples, as well.

Thanks again.

George

[quoted text, click to view]
RE: newbie question on finding and editing yingzile NO[at]SPAM online.microsoft.com
1/31/2005 8:40:13 PM
Hi Geoge,

I'd suggest you use Xml DOM and XmlElement. Take a look at these
documentations:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemxmlxmldocumentmemberstopic.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemxmlxmlelementmemberstopic.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemxmlxmlelementclasssetattributetopic.asp

You can do something like this(This is C# sintax, VB syntax is similar):

XmlDocument xmlDoc = new XmlDocument();
XmlTextReader reader = new XmlTextReader("C:\temp\booklist.xml");
reader.XmlResolver = null;
xmlDoc.Load(reader);

//get all the book nodes using XPath
XmlNodeList bookNodes = xmlDoc.SelectNodes("//book");

//Iterate each book nodes
foreach (XmlElement book in bookNodes)
{
//your test statements
//you can update the //book attributes and element
book.SetAttributes("ISBN", <new value here>);
//get child Element
XmlElement info = book.SelectSingleNode("./info");
}

Assume from the following XML file:

<BookList>
<book ISBN="">
<title/>
<authors><author/></authors>
<info/>
................
<book>
</BookList>

Hope this helps!
--Yingzi Le
LongHorn SDK Team

This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
[quoted text, click to view]


AddThis Social Bookmark Button