Add XML Nodes to an existing Doc.

I have a base XML document with multiple nodes.  I need to clone the layout for an existing node and multiple it in a foreach loop.  

 

How do I add in multiple nodes for each arrest?  
<Arrests>
<Arrest>

      <CaseNumber></CaseNumber>

      <ArrestedOn></ArrestedOn>

      <ChargeClass></ChargeClass>

      <ChargeDescription></ChargeDescription>

      <Sentenced></Sentenced>

      <DaysInJail></DaysInJail>

      <DisposedOn></DisposedOn>

      <DispositionCode></DispositionCode>

      <DispositionDescription></DispositionDescription>
</Arrest>
</Arrrests>
 

For some customers I have a number around 40, for others only 1-2-3  :)

I am trying this but I am missing the basic point.

XmlNodeList aIns = Node.SelectNodes("/Person/Arrests");

 XmlNode NewNode = aIns[0].Clone();          

 
what am I doing wrong?

TIA

 
LVL 1
srussell705Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
What version of the .Net Framework are you using
Visual Studio 2003 = .Net 1.1
Visual Studio 2005 = .Net 2.0
Visual Studio 2008 = .Net 3.5
0
Fernando SotoRetiredCommented:
Hi srussell705;

The following code snippet will work in the .Net 2.0 and above, VS 2005 and VS 2008.

Fernando
XmlDocument doc = new XmlDocument();
doc.Load("Test.xml");

// Setup the inner text for each node to update the data
List<string> fieldsValues = new List<string>() {"2", DateTime.Now.AddDays(-10).ToShortDateString(), "1",
    "B&E","1 year", "1 year", DateTime.Now.ToShortDateString(), "0", "Complete"};
// Reference to the root node
XmlNode root = doc.SelectSingleNode("//Arrests");
// Clone first arrest to create the arrest node and all its children
XmlNode arrest = doc.SelectSingleNode("//Arrests/Arrest").Clone();
// Update the cloned nodes
int idx = 0;
foreach (XmlNode n in arrest.ChildNodes)
{
    n.InnerText = fieldsValues[idx];
    idx++;
}
// Attach the new Arrest node to the end of all its children
root.AppendChild(arrest);
// Save the new document
doc.Save("TestNew.xml");

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
srussell705Author Commented:
Thanks.  Worked like a charm.  

I missed the whole List idea for the stuffing.  
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
XML

From novice to tech pro — start learning today.