adding a node in XML file

V

Vicente García

hi,
I have a xml file as shown:

<?xml version="1.0" encoding="utf-8"?>
<PIM>
<fragments>
</fragments>
</PIM>
</xml>

I must add children in the fragments node such as:

<fragments>
<element name="name1">hello</element>
<element name="name2">hello 2</element>
<element name="name3">hello 3</element>
....
<fragments>

The problem is that I am not be able to add children using XmlDocument,
XmlElement nor XmlNode...May anyone help me with this issue?

Thanks in advance
regards
 
V

Vadym Stetsyak

Hello, Vicente!

[skipped]

VG> The problem is that I am not be able to add children using XmlDocument,
VG> XmlElement nor XmlNode...May anyone help me with this issue?

What's the problem? Give code sample, where you're trying to add children node.

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
 
V

Vicente Garcia

Thanks for the reply Vadym

This is my snippet code:
....
XmlNode fragments = root.SelectSingleNode("fragments"); //now I am in
fragments
....//My problem is that I don't know how to declare the node element and I
didn't find that on the Web
fragments.AppendChildNode(node); //I must add nodes

Apologies if my explanation insn't enough good, This is because of my mother
tongue isn't English
Thanks a lot!
 
G

Guest

Vicente García said:
hi,
I have a xml file as shown:

<?xml version="1.0" encoding="utf-8"?>
<PIM>
<fragments>
</fragments>
</PIM>
</xml>

I must add children in the fragments node such as:

<fragments>
<element name="name1">hello</element>
<element name="name2">hello 2</element>
<element name="name3">hello 3</element>
....
<fragments>

The problem is that I am not be able to add children using XmlDocument,
XmlElement nor XmlNode...May anyone help me with this issue?

Thanks in advance
regards

Vicente,
What technicial limitation prohibits you from using the XmlDocument object
to manipulate your doucment (other than the fact that this document is not
valid xml)?
Unless you can provide sound logical reasons and technical limitations there
isn't a solution for you here - only a hack.

The XmlDocument object conforms to the xml standards, unless you know
exactly what you are doing you should avoid the "I'm smarter than the
framework developers" approach to xml manipulation. Don't take offence to
this either. I've seen way to many solvable problems that people just don't
take the time to fix correctly. Some just apply a hack to get it working and
never go back. This will cause more problems in the long run.

<?xml version="1.0" encoding="utf-8"?>
<PIM>
<fragments>
</fragments>
</PIM>
</xml>
<!-- </xml> is not valid -
The <?xml version="1.0" ?> is a processing instruction and is required for
all xml documents, it doesn't required any ending tag, let alone one that was
never defined.
 
V

Vadym Stetsyak

Hello, Vicente!

VG> This is my snippet code:
VG> ...
VG> XmlNode fragments = root.SelectSingleNode("fragments"); //now I am in
VG> fragments
VG> ...//My problem is that I don't know how to declare the node element
VG> and I didn't find that on the Web

//assume that we have XmlDocument xmlDoc

XmlElement newElement = xmlDoc.CreateElement("element");
newElment.SetAttribute("name", "nameXXX");
fragments.AppendChild(newElement );

VG> Apologies if my explanation insn't enough good, This is because of my
VG> mother tongue isn't English

You're not alone ::cool:

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
 
V

Vicente Garcia

Thanks a million!

I've just run it and it works fine :)
I was very confused because of the examples I found didn't serve

regards
 
V

Vicente García

hi Jared
I type the last </xml> but it isn't in my code, I didn't realize
I could use XmlDocument, in fact this is my intention but I didn't Know how
to use it in this situation because my code always failed, but now Vadim
shows me how to use it.

many thanks
regards
 

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

Top