H
H
Haze Max2016-10-22 18:42:24
C++ / C#
Haze Max, 2016-10-22 18:42:24

How to deal with XML with weak links?

Guys explain how to work with XML
If I write

elem = XDoc.CreateElement("sendpost");
XmlText xetxt = XDoc.CreateTextNode(text);
elem.AppendChild(xetxt);
XSession.AppendChild(elem);

Then everything works as it should
for me. In this case, I only add text without the sendpost element.
XSession.AppendChild(XDoc.CreateElement("sendpost").AppendChild(XDoc.CreateTextNode(resp)));

Perhaps I am not working correctly with XML, but the first method seems to me not convenient and cumbersome, unlike the second.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2016-10-23
@hazemax

I recommend using XLinq, a new take on XML in .NET. Below is an example:

using System;
using System.Xml;
using System.Xml.Linq;
          
public class Program
{
  public static void Main()
  {
    string text = "Hello World";
    var doc = XDocument.Parse(@"<?xml version=""1.0"" encoding=""utf-8""?><root />");
    doc.Root.Add(new XElement("sendpost", text));
    
    Console.WriteLine(doc.ToString());
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question