Mahendra
Mahendra

Reputation: 337

How to add a new XmlElement to existing XML in C#?

I have a XML stored in below format:

(XmlDocument)JsonConvert.DeserializeXmlNode(requestBody, "root");

<root>
    <NumberActa>20659</NumberActa>
    <DegreeDate>09/10/2018</DegreeDate>
    <StudentList>
        <CostsCenter>ABK015q</CostsCenter>
        <DocumentType>C.C h.</DocumentType>
        <Names>LISSET MARCELA</Names>
    </StudentList>
    <StudentList>
        <CostsCenter>ABCDE</CostsCenter>
        <DocumentType>C.C h.</DocumentType>
        <Names>MARCELA</Names>
    </StudentList>
</root>

I have an room element, instead of this element i need to add <DA><DE></DE></DA> and i need it in below format

   <DA>
      <DE>
        <NumberActa>20659</NumberActa>
        <DegreeDate>09/10/2018</DegreeDate>
        <StudentList>
            <CostsCenter>ABK015q</CostsCenter>
            <DocumentType>C.C h.</DocumentType>
            <Names>LISSET MARCELA</Names>
        </StudentList>
        <StudentList>
            <CostsCenter>ABCDE</CostsCenter>
            <DocumentType>C.C h.</DocumentType>
            <Names>MARCELA</Names>
        </StudentList>
       </DE>
    </DA>

how to add a element?

Upvotes: 1

Views: 836

Answers (2)

Mahendra
Mahendra

Reputation: 337

Thank you :), i found this below answer and it is working fine now.

XmlDocument XmldocNew = new XmlDocument();
XmlElement newRoot = XmldocNew.CreateElement("DE");
XmldocNew.AppendChild(newRoot);
//newRoot.InnerXml = doc.DocumentElement.InnerXml;
newRoot.InnerXml = doc.DocumentElement.OuterXml;

Upvotes: 2

Mehrdad Dowlatabadi
Mehrdad Dowlatabadi

Reputation: 1335

You can create new node and append new node to it then for each child append the child to node :

string source= @" <root>
<NumberActa>20659</NumberActa>
<DegreeDate>09/10/2018</DegreeDate>
<StudentList>
    <CostsCenter>ABK015q</CostsCenter>
    <DocumentType>C.C h.</DocumentType>
    <Names>LISSET MARCELA</Names>
</StudentList>
<StudentList>
    <CostsCenter>ABCDE</CostsCenter>
    <DocumentType>C.C h.</DocumentType>
    <Names>MARCELA</Names>
</StudentList>
</root>";
var document = new XmlDocument();
document.LoadXml(source);
XmlNode oldRoot = document.SelectSingleNode("root");
XmlNode newRoot = document.CreateElement("DA");
XmlNode second = document.CreateElement("DE");

document.ReplaceChild(newRoot, oldRoot);
newRoot.AppendChild(second);

foreach (XmlNode childNode in oldRoot.ChildNodes)
{
    second.AppendChild(childNode.CloneNode(true));
}

var result = document.InnerXml;

Result:

<DA>
    <DE>
        <NumberActa>20659</NumberActa>
        <DegreeDate>09/10/2018</DegreeDate>
            <StudentList>
                <CostsCenter>ABK015q</CostsCenter>
                <DocumentType>C.C h.</DocumentType>
                <Names>LISSET MARCELA</Names>
            </StudentList>
            <StudentList>
                <CostsCenter>ABCDE</CostsCenter>
                <DocumentType>C.C h.</DocumentType>
                <Names>MARCELA</Names>
            </StudentList>
    </DE>
</DA>

Upvotes: 2

Related Questions