White space in XmlElement in C# .Net

前端 未结 3 1212
青春惊慌失措
青春惊慌失措 2021-01-19 22:44

XmlElement child = doc.CreateElement(element);

Where doc is an object of XmlDocument. When the code executes the above line wi

3条回答
  •  不思量自难忘°
    2021-01-19 23:17

    I suppose you want an element with the value "Tom and Jerry", which is fine.

    It is part of the XML syntax that you cannot have a space in the name of an element or attribute.

    A possible method:

    XmlElement child = doc.CreateElement("cartoon");
    child.InnerText = "Tom and Jerry";
    

    which produces

    Tom and Jerry
    

    Aside, consider XDocument when you can. Much easier than XmlDocument.

    XElement child = new XElement("cartoon", "Tom and Jerry");
    

提交回复
热议问题