473,387 Members | 1,588 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.

Adding an element at each parent node at a specific position

I need to add element "REFERENCE" for every parent node after STATUS element.
<ORDER> <DETAIL> <STATUS>1</STATUS> <TYPE>1</TYPE> </DETAIL> <DETAIL> <STATUS>2</STATUS> <TYPE>3</TYPE> </DETAIL> </ORDER>

Expand|Select|Wrap|Line Numbers
  1. System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
  2.             doc.Load(filename);
  3.  
  4.             //Specify root where the new element needs to added
  5.             System.Xml.XmlNodeList WHORDER = doc.SelectNodes("//ORDER/DETAIL");
  6.             System.Xml.XmlNode STATUS = doc.SelectSingleNode("/ORDER/DETAIL/STATUS");
  7.  
  8.             if (STATUS != null)
  9.             {                
  10.                 foreach (System.Xml.XmlNode item in WHORDER)
  11.                 {
  12.                     System.Xml.XmlElement REFERENCE = doc.CreateElement("REF");
  13.                     REFERENCE.InnerText = 'A';
  14.  
  15.                     System.Xml.XmlNode commonParent = STATUS.ParentNode;
  16.                     commonParent.InsertAfter(REFERENCE, STATUS);                   
  17.                 }
  18.             }
  19.  
the output looks like
<ORDER> <DETAIL> <STATUS>1</STATUS> <REF>A</REF> <REF>A</REF> <TYPE>1</TYPE> </DETAIL> <DETAIL> <STATUS>2</STATUS> <TYPE>3</TYPE> </DETAIL> </ORDER>

Can anyone help me with what am i doing wrong?
Sep 25 '18 #1
0 1124

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

Similar topics

1
by: discomiller | last post by:
Mario Mueller: Hello *, radiobuttons belong to other radiobuttons by the "name="any_value"" attribut. Thats a fakt. I got the following XML:...
2
by: john smith | last post by:
Hello, I am having trouble getting the parent node of an xml document using xslt. I am trying to return the node wine, but this does not return wine but the element values under wine. I...
1
by: arvindsd | last post by:
Hi all, My XML file is as shown below: <?xml version="1.0" ?> <messages> <message> <code>100</code> <text>Unknown application error</text> </message>
2
by: David Lei | last post by:
Hi, How do I add a new parent node to the existing nodes? I have an existing XmlDocument object like this: <item> <something>ABC</something> <item> <item> <something>DEF</something>
3
by: MrNobody | last post by:
Ok, so I'm absoultely positive this was working last night, but today I'm getting this error in my program when I try to add a child node to a parent node of a tree... The error says: ...
14
by: neerajb | last post by:
Hi, I am having an XML document(input.xml) which is showing the menu heirarchy used in my application.My requirement is to add "submenu" tag to those menuitems who are having the child menuitems...
4
by: MA | last post by:
Hi, How to access the total number of child nodes from a parent node. For example, I would like to get the total number of child nodes from <parent1and <parent2node. The SelectNodes method...
0
by: jalicej | last post by:
In my C# web application i have a treeview control with showcheckboxes = All . So for each node the check box is shown. When i check on the parent node all its child nodes should be checked...
4
by: jmDesktop | last post by:
I have searched everywhere and tried several things. I have a treeview with and want to be able to only select a parent node. For example: root //don't want to drag this -parent1 //yes, drag...
1
by: SWETA123 | last post by:
I will very helpful to you if you can help me out in my below problem. I am not getting idea what should be the right approach for this. Here is my problem. I have below (sample)xml. <Rules>...
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: 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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.