Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / November 2006

Tip: Looking for answers? Try searching our database.

Iterate XML Document and Add Attirbute?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
xeroxero - 02 Nov 2006 17:28 GMT
I have an XML document with this as a sample fragment:

<?xml version="1.0" encoding="utf-16"?>
<directory path="c:\\test\\directory">
<file name="fileA.doc" />
<file name="fileB.txt" />
</directory>

I would like to iterate over the document, find any element that is
named 'file', and add an attribute 'size' if it doesn't already exist.
Can an XPathNavigator be used for that purpose, and if so how? I am
not sure about which tool to use, since it seems like I'll modify the
collection while it's in-use.

Thanks.
Vadym Stetsyak - 02 Nov 2006 20:57 GMT
Hello, xeroxero!

You can use XML DOM model here and XPath.

So, the code can look like this:
XmlDocument xmlDoc= new XmlDocument();
xmlDoc.Load(xml);

XmlNodeList files = xmlDoc.SelectNodes("directory/file");
foreach(XmlNode file in files)
{
   //create attribute 'size' if it doesn't exist
}

You wrote  on Thu, 02 Nov 2006 11:28:04 -0500:

x> I have an XML document with this as a sample fragment:

x> <?xml version="1.0" encoding="utf-16"?>
x> <directory path="c:\\test\\directory">
x> <file name="fileA.doc" />
x> <file name="fileB.txt" />
x> </directory>

x> I would like to iterate over the document, find any element that is
x> named 'file', and add an attribute 'size' if it doesn't already
x> exist.
x> Can an XPathNavigator be used for that purpose, and if so how? I am
x> not sure about which tool to use, since it seems like I'll modify the
x> collection while it's in-use.

x> Thanks.

--
With best regards, Vadym Stetsyak.
Blog: http://vadmyst.blogspot.com
xeroxero - 03 Nov 2006 21:30 GMT
Thanks, but that won't exactly do it. Sometimes, <directory> elements
can be in another directory. That's why I thought I need to use an
XPathNavigator.

What can I do?

Thanks.

>Hello, xeroxero!
>
[quoted text clipped - 28 lines]
>
>x> Thanks.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.