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

Append an xml file

I have a xml file that looks as such
Expand|Select|Wrap|Line Numbers
  1. <people>
  2.     <person name="a" id"123">
  3.     <person name="z" id"312">
  4. </people>
  5.  
and I want to add a person with the name of "b" but put in inbetween "a" and "z" to keep it alphabetical.
I was able to add to the bottom so it would look like
Expand|Select|Wrap|Line Numbers
  1. <people>
  2.     <person name="a" id"123">
  3.     <person name="z" id"312">
  4.     <person name="b" id"452">
  5. </people>
  6.  
with this code

Expand|Select|Wrap|Line Numbers
  1. XmlDocument xmlDoc = new XmlDocument();
  2.             xmlDoc.Load(xml);
  3.             XmlNode node = xmlDoc.SelectSingleNode("/people");
  4.             XmlElement el = xmlDoc.CreateElement("person");
  5.             el.SetAttribute("name", name);
  6.             el.SetAttribute("id", id);      
  7.             node.AppendChild(el);
  8.             xmlDoc.Save(xmlFile);
So does anyone know how I can modify this to allow me to insert into the middle?
Dec 1 '08 #1
4 1081
Plater
7,872 Expert 4TB
I think you need to go one step further into the tree and find the XmlNode that goes with <person name="a" id"123">
And then use InsertAfter()
Dec 1 '08 #2
@Plater
Tried to do that and insert after doesn't accept an XmlElement
Dec 1 '08 #3
Plater
7,872 Expert 4TB
It inherits from it, type-cast it.

Expand|Select|Wrap|Line Numbers
  1. Inheritance Hierarchy:
  2. System.Object 
  3.    System.Xml.XmlNode 
  4.      System.Xml.XmlLinkedNode 
  5.       System.Xml.XmlElement
  6.  
Dec 1 '08 #4
Woot thanks for the help. If anyone wants my finished code this is what it looked like
Expand|Select|Wrap|Line Numbers
  1. XmlDocument xmlDoc = new XmlDocument();
  2.             xmlDoc.Load(xml);
  3.             XmlNodeList nodes = xmlDoc.SelectNodes("/people/person");
  4.             foreach (XmlNode node in nodes)
  5.             {
  6.                 XmlAttributeCollection attCol = node.Attributes;
  7.                 if (attCol[0].Value.ToString().CompareTo(newName) >= 0)
  8.                 {
  9.                     XmlElement el = xmlDoc.CreateElement("person");
  10.                     el.SetAttribute("name",newName);
  11.                     el.SetAttribute("id",id);
  12.                     xmlDoc.SelectSingleNode("/people").InsertBefore((XmlNode)el, node);
  13.                     break;
  14.                 }
  15.  
  16.             }
  17.             xmlDoc.Save(xmlFile);
Dec 1 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Nico Grubert | last post by:
Hi there, I would like to open an existing file that contains some lines of text in order to append a new line at the end of the content. My first try was: >>> f = open('/tmp/myfile', 'w')...
3
by: Jonathan Buckland | last post by:
Can someone give me an example how to append data without having to load the complete XML file. Is this possible? Jonathan
2
by: Matrix | last post by:
Greetings I want to append records in a XML file,I use XMLTextWriter,but i found it only create a XML file,not append records in exist XML file.why?...
4
by: Amy L. | last post by:
It is my understanding that in order to append XML to a file you would have to read the file in as an XML document and insert the node. However, what if the file is close to 1GB in size? Would you...
14
by: vbMark | last post by:
Greetings, This seems like it should be simple but I can't figure out how to do this. I just want to append binary file 2 on to the end of binary file 1. Sample code please? Thanks!
5
by: Simone M | last post by:
Here is my problem: I need to append a string to a file every time the user downloads this file. The string is different for every user. I would like to append the string to the file via ftp. But...
3
by: Mohan | last post by:
Hi I face a problem with the stl string append on our 32-bit SLES9 machine . (It is not happening on SLES9 64-bit machines and on SuSE 10). When we read a 179MB file into the memory,...
2
by: Kevien Lee | last post by:
Hi , I had a strang problam ,when i use StreamWriter to append to a file,i found that if i don't close the StreamReader it couldn't write the data into file,the code as folllow that: class...
2
by: Gaby Sandoval | last post by:
I have this code. The user can read teh record from the text file just fine. They can click the NEXT button and it reads the next record (which is just the next line from the text file). If the...
2
by: Curious | last post by:
I want to write to an output file, "C:\\temp\\debug.txt": If the file exists, append the new content to the end of the file (instead of overwriting the current content). Therefore, I want to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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,...

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.