Как добавить новый XmlElement в существующий XML в C #? - PullRequest
0 голосов
/ 12 февраля 2019

У меня есть XML, хранящийся в следующем формате:

(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>

У меня есть элемент комнаты, вместо этого элемента мне нужно добавить <DA><DE></DE></DA>, и мне нужноэто в формате ниже

   <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>

как добавить элемент?

Ответы [ 2 ]

0 голосов
/ 12 февраля 2019

Спасибо :), я нашел этот ответ ниже, и теперь он работает нормально.

XmlDocument XmldocNew = new XmlDocument();
XmlElement newRoot = XmldocNew.CreateElement("DE");
XmldocNew.AppendChild(newRoot);
//newRoot.InnerXml = doc.DocumentElement.InnerXml;
newRoot.InnerXml = doc.DocumentElement.OuterXml;
0 голосов
/ 12 февраля 2019

Вы можете создать новый узел и добавить новый узел к нему, а затем для каждого дочернего элемента добавить дочерний элемент к узлу:

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;

Результат:

<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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...