Reputation:
I need to create an XML and return it as a string. Can anyone tell me how to create the following XML using XmlDocument
?
<outputs>
<output name="" value="" type=""></output>
<output name="" value="" type=""></output>
<output name="" value="" type=""></output>
</outputs>
UPDATE
var xmlDocument = new XmlDocument();
var xmlNode=xmlDocument.CreateNode(XmlNodeType.XmlDeclaration,"outputs","namespace");
xmlDocument.AppendChild(xmlNode);
var xmlElement = xmlDocument.CreateElement("", "output", "");
xmlDocument.AppendChild(xmlElement);
Upvotes: 3
Views: 28746
Reputation: 3778
//Create XmlDocument
XmlDocument xmlDoc = new XmlDocument();
//Create the root element
XmlNode outputsElement = xmlDoc.CreateElement("outputs");
//Create the child element
XmlElement Element = xmlDoc.CreateElement("output");
//Create "name" Attribute
XmlAttribute nameAtt = xmlDoc.CreateAttribute("name");
Element.Attributes.Append(nameAtt);
//Create "value" Attribute
XmlAttribute valueAtt = xmlDoc.CreateAttribute("value");
Element.Attributes.Append(valueAtt);
//Create "type" Attribute
XmlAttribute typeAtt = xmlDoc.CreateAttribute("type");
Element.Attributes.Append(typeAtt);
//Append child element into root element
outputsElement.AppendChild(Element);
and to return it as string: xmlDoc.OuterXml;
Upvotes: 3
Reputation: 125650
I think you should consider using XDocument
instead of XmlDocument
:
var doc = new XDocument(new XElement("outputs",
new XElement("output",
new XAttribute("name", ""),
new XAttribute("value", ""),
new XAttribute("type", "")),
new XElement("output",
new XAttribute("name", ""),
new XAttribute("value", ""),
new XAttribute("type", "")),
new XElement("output",
new XAttribute("name", ""),
new XAttribute("value", ""),
new XAttribute("type", ""))));
You can than easily write the xml into a string:
var myXmlString = doc.ToString();
You can also achieve the same goal with XDocument.Parse()
static method:
var doc = XDocument.Parse("<outputs><output></output> (...) </outputs>");
You can add content using loop as well:
var doc = new XDocument(new XElement("outputs"));
var root = doc.Root;
foreach(var o in outputs)
{
root.Add(new XElement("output",
new XAttribute("name", o.Name),
new XAttribute("value", o.Value),
new XAttribute("type", o.Type)));
}
Upvotes: 12
Reputation: 3069
string str = "<outputs><output name=\"\" value=\"\" type=\"\"></output><output name=\"\" value=\"\" type=\"\"></output><output name=\"\" value=\"\" type=\"\"></output></outputs>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(str);
And for creating a string again.
string toString = string.Empty;
using (StringWriter stringWriter = new StringWriter())
{
XmlTextWriter xmlTextWriter = new XmlTextWriter(stringWriter);
doc.WriteTo(xmlTextWriter);
toString = stringWriter.ToString();
}
Upvotes: 1
Reputation: 13388
This will help you a lot, it is a great example about how to Read and Write to an xml file:
Example code about how to Create elements and a whole XML file, using c# code:
static void Main(string[] args)
{
// Create a new file in C:\\ dir
XmlTextWriter textWriter = new XmlTextWriter("C:\\myXmFile.xml", null);
// Opens the document
textWriter.WriteStartDocument();
// Write comments
textWriter.WriteComment("First Comment XmlTextWriter Sample Example");
textWriter.WriteComment("myXmlFile.xml in root dir");
// Write first element
textWriter.WriteStartElement("Student");
textWriter.WriteStartElement("r", "RECORD", "urn:record");
// Write next element
textWriter.WriteStartElement("Name", "");
textWriter.WriteString("Student");
textWriter.WriteEndElement();
// Write one more element
textWriter.WriteStartElement("Address", ""); textWriter.WriteString("Colony");
textWriter.WriteEndElement();
// WriteChars
char[] ch = new char[3];
ch[0] = 'a';
ch[1] = 'r';
ch[2] = 'c';
textWriter.WriteStartElement("Char");
textWriter.WriteChars(ch, 0, ch.Length);
textWriter.WriteEndElement();
// Ends the document.
textWriter.WriteEndDocument();
// close writer
textWriter.Close();
}
Upvotes: 1