create xml element

R

Ryan Liu

Hi,

I know I need create a DataRow from a DataTable for the schema reason.

Why must I create a XmlElement in a XmlDocument?

I already have a string, I was trying to use XmlDocument.CreateElement("Q")
method to create a "fake" or "Empty" XmlElement and set this element's
OuterXml, but OuterXml is read only.

I can set inner Xml, but I think I will lost attributs of element Q. I will
try that out.

But what is best practice to create an element from a given complete node
string? Should I try ImportNode()?

And same as new DataRow does not belong to DataTable, after I create the
element, it is not part of the XmlDocument unless I append to one of its
chile node, right?

Microsoft SDK is great resouce, but some time I still feel it tells
something, but not deep and clear enough, I need write small test programs
and ask others to know what really is its accurate meaning.

Thanks a lot!
Ryan
 
M

Martin Honnen

Ryan Liu wrote:

But what is best practice to create an element from a given complete node
string?

You can use a DocumentFragment node and set its InnerXml property e.g.

XmlDocument xmlDocument = new XmlDocument();
// load some example contents
xmlDocument.LoadXml(@"<gods />");

XmlDocumentFragment fragment = xmlDocument.CreateDocumentFragment();
fragment.InnerXml = @"<god>Kibo</god><god>Xibo</god>";
xmlDocument.DocumentElement.AppendChild(fragment);

xmlDocument.Save(Console.Out);

And there is the ReadNode method e.g.

XmlDocument xmlDocument = new XmlDocument();
// some example contents
xmlDocument.LoadXml(@"<gods />");

XmlNode node = xmlDocument.ReadNode(new XmlTextReader(new
StringReader(@"<god>Kibo</god>")));
xmlDocument.DocumentElement.AppendChild(node);

xmlDocument.Save(Console.Out);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top