Repeat XML node
-
-
I am having an XMl like below <Details> <det id=1 name="abc"> </Details> Now i want to take the above xml as input and to repeat the node like below using C# <Details> <det id=1 name="abc"/> <det id=2 name="abc"/> <det id=3 name="abc"/> </Details>
-
I don't understand what your problem is. What code are you using to create the original XML, and what code have you tried to create the repeat items?
Rich, In c#.Original xml is a string which I get like below <details> <detail id="1" name="x"/> </details> I want to take that xml string and to repeat the nodes foreach xml nodes <details> <detail id="1" name="x"/> <detail id="2" name="x"/> <detail id="3" name="x/> </details> id=2 and id=3 is repeated.want to do it in C#
-
Rich, In c#.Original xml is a string which I get like below <details> <detail id="1" name="x"/> </details> I want to take that xml string and to repeat the nodes foreach xml nodes <details> <detail id="1" name="x"/> <detail id="2" name="x"/> <detail id="3" name="x/> </details> id=2 and id=3 is repeated.want to do it in C#
That can be easly done using XmlDocument. You know that class? Have you already tried something or you want us to give all the code to you (that you won't get here, I'm afraid)?
-
Rich, In c#.Original xml is a string which I get like below <details> <detail id="1" name="x"/> </details> I want to take that xml string and to repeat the nodes foreach xml nodes <details> <detail id="1" name="x"/> <detail id="2" name="x"/> <detail id="3" name="x/> </details> id=2 and id=3 is repeated.want to do it in C#
kurangu wrote:
In c#.Original xml is a string which I get like below
Where do you get this from, a const string, XmlReader, TextReader?
kurangu wrote:
I want to take that xml string and to repeat the nodes
I ask again, what code have you tried; please help us to understand better what you are actually doing in your code?
-
kurangu wrote:
In c#.Original xml is a string which I get like below
Where do you get this from, a const string, XmlReader, TextReader?
kurangu wrote:
I want to take that xml string and to repeat the nodes
I ask again, what code have you tried; please help us to understand better what you are actually doing in your code?
HI, It's a constant string. string a = "<SecDetails><Security PrimarySecID=\"abcd\" Price=\"12\"/></SecDetails>"; XmlDocument originalXml = new XmlDocument(); originalXml.LoadXml(a); XmlNodeList nodelist = originalXml.SelectNodes("/SecDetails/Security"); foreach (XmlNode xn in nodelist) { here I should be able to repeat the node for (int i=1; i<=10;i++) { I am struck here. } }
-
HI, It's a constant string. string a = "<SecDetails><Security PrimarySecID=\"abcd\" Price=\"12\"/></SecDetails>"; XmlDocument originalXml = new XmlDocument(); originalXml.LoadXml(a); XmlNodeList nodelist = originalXml.SelectNodes("/SecDetails/Security"); foreach (XmlNode xn in nodelist) { here I should be able to repeat the node for (int i=1; i<=10;i++) { I am struck here. } }
Look at the XmlNode class, in particular, the ParentNode property and the AppendChild and CloneNode methods. You should have now all the element you need to perform your task.
-
HI, It's a constant string. string a = "<SecDetails><Security PrimarySecID=\"abcd\" Price=\"12\"/></SecDetails>"; XmlDocument originalXml = new XmlDocument(); originalXml.LoadXml(a); XmlNodeList nodelist = originalXml.SelectNodes("/SecDetails/Security"); foreach (XmlNode xn in nodelist) { here I should be able to repeat the node for (int i=1; i<=10;i++) { I am struck here. } }
Now I'm even more confused, I don't see a relation between what you have posted here, and the XML statements from your original post. If you are saying that you don't know how to format a string in C# then I suggest you look at the documentation for the
String
class. If you are unsure of the Xml data formats then look at the items referred to by Mirko1980. If you are having trouble understanding thefor
orforeach
statements then take a look at the C# documentation.