473,396 Members | 1,773 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,396 software developers and data experts.

Need help modifying XML file...

I have this xml file...

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<images>
<pic>
<image>http://www.somesite.com/mypic.jpg</image>
<caption>Picture 1 is here</caption>
</pic>
<pic>
<image>http://www.somesite.com/mypic2.jpg</image>
<caption>Picture 2 is here</caption>
</pic>
</images>

Using VB.NET how could I add, edit and remove a <pic> item, consisting of an
<image> and <caption>? Any sample code you could post? I'm an xml newb.

The xml file is resident on the root of this website.

Thanks!
Nov 12 '05 #1
2 3320
VB Programmer wrote:
I have this xml file...

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<images>
<pic>
<image>http://www.somesite.com/mypic.jpg</image>
<caption>Picture 1 is here</caption>
</pic>
<pic>
<image>http://www.somesite.com/mypic2.jpg</image>
<caption>Picture 2 is here</caption>
</pic>
</images>

Using VB.NET how could I add, edit and remove a <pic> item, consisting of an
<image> and <caption>? Any sample code you could post? I'm an xml newb.


XmlDocument doc = new XmlDocument();
doc.Load(..);

To add a new Item with an attribute and an image-Child node with some
text content use:

XmlElement newItem = doc.CreateElement("pic");
XmlElement newImage = doc.CreateElement("image");
newImage.AppendChild( doc.CreateTextNode("http://...") );
newItem.AppendChild( newImage );
doc.DocumentElement.AppendChild( newItem );

There are many ways to remove an item. Most flexible is to use XPath:

XmlElement e = doc.SelectSingleNode("/images/pic[starts-with(caption,
'Picture 1')]") as XmlElement;
e.ParentNode.RemoveChild( e );

Or you could remove the first pic:

doc.DocumentElement.RemoveChild( doc.DocumentElement.FirstChild );

Editing nodes is mostly just adding and removing their child nodes. Only
Text-Nodes can be directly altered.
Best thing is to see the documentation:
<http://msdn2.microsoft.com/library/System.Xml.XmlDocument>
<http://msdn2.microsoft.com/library/System.Xml.XmlNode>
<http://msdn2.microsoft.com/library/System.Xml.XmlElement>

--
Pascal Schmitt
Nov 12 '05 #2
Great response! Thanks.

"Pascal Schmitt" <ne*******@cebra.nu> wrote in message
news:OK**************@TK2MSFTNGP12.phx.gbl...
VB Programmer wrote:
I have this xml file...

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<images>
<pic>
<image>http://www.somesite.com/mypic.jpg</image>
<caption>Picture 1 is here</caption>
</pic>
<pic>
<image>http://www.somesite.com/mypic2.jpg</image>
<caption>Picture 2 is here</caption>
</pic>
</images>

Using VB.NET how could I add, edit and remove a <pic> item, consisting of
an <image> and <caption>? Any sample code you could post? I'm an xml
newb.


XmlDocument doc = new XmlDocument();
doc.Load(..);

To add a new Item with an attribute and an image-Child node with some text
content use:

XmlElement newItem = doc.CreateElement("pic");
XmlElement newImage = doc.CreateElement("image");
newImage.AppendChild( doc.CreateTextNode("http://...") );
newItem.AppendChild( newImage );
doc.DocumentElement.AppendChild( newItem );

There are many ways to remove an item. Most flexible is to use XPath:

XmlElement e = doc.SelectSingleNode("/images/pic[starts-with(caption,
'Picture 1')]") as XmlElement;
e.ParentNode.RemoveChild( e );

Or you could remove the first pic:

doc.DocumentElement.RemoveChild( doc.DocumentElement.FirstChild );

Editing nodes is mostly just adding and removing their child nodes. Only
Text-Nodes can be directly altered.
Best thing is to see the documentation:
<http://msdn2.microsoft.com/library/System.Xml.XmlDocument>
<http://msdn2.microsoft.com/library/System.Xml.XmlNode>
<http://msdn2.microsoft.com/library/System.Xml.XmlElement>

--
Pascal Schmitt

Nov 12 '05 #3

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

Similar topics

2
by: pancho | last post by:
Greetings, I need help configuring/building PHP3 with MySQL as a DSO on a Solaris 8 box - this module is needed to host some existing sites I will be migrating Note. I built PHP4 from source and...
2
by: yvan | last post by:
I was wondering if someone here could help me with modifying this PHP template so that it does something very specific for me. My knowledge of PHP is very limited (I'm more of a Cold Fusion...
5
by: Benne Smith | last post by:
Hi, I have three enviroments; a development, a testing and a production enviroment. I'm making a big application (.exe), which uses alot of different webservices. I don't use the webservices...
1
by: Max Khitrov | last post by:
Hello everyone, I'm working on a VS .NET add-in that will allow developers to use Subversion software from within the IDE (much like Source Safe). Ideally, I would like for my plug-in to be able...
2
by: Yavuz Bogazci | last post by:
Hi, i am reading the LastAccessTime, LastWriteTime and DateofCreation of a file. The second and third are working well, but each time i want to read the LastAccessTime, its set to "now". How can...
7
by: Enigma Curry | last post by:
I need to store a large number of files in an archive. From Python, I need to be able to create an archive, put files into it, modify files that are already in it, and delete files already in it. ...
2
by: rk | last post by:
I have the following library.xml file coming from a system, this can't be modified. ____________________________________________________________________________ <?xml version="1.0"...
0
by: svallarian | last post by:
C# beginner here. I'm writing serialized records to a file, and I'm able to add and read them back to the program no problem. However, when I try to modify the record by reading the record,...
24
by: allpervasive | last post by:
hi all, this is reddy, a beginner to c lang,,here i have some problems in reading and modifying the contents of a file,, hope you can help to solve this problem. Here i attach the file to be...
5
by: IUnknown | last post by:
Ok, we are all aware of the situation where modifying the folder structure (adding files, folders, deleting files, etc) will result in ASP.NET triggering a recompilation/restart of the application....
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: 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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.