3

我需要创建一个 XML 并将其作为字符串返回。谁能告诉我如何使用创建以下 XML XmlDocument

<outputs>
  <output name="" value="" type=""></output>
  <output name="" value="" type=""></output>
  <output name="" value="" type=""></output>
</outputs>

更新

var xmlDocument = new XmlDocument();

            var xmlNode=xmlDocument.CreateNode(XmlNodeType.XmlDeclaration,"outputs","namespace");
            xmlDocument.AppendChild(xmlNode);

            var xmlElement = xmlDocument.CreateElement("", "output", "");
            xmlDocument.AppendChild(xmlElement);
4

4 回答 4

11

我认为您应该考虑使用XDocument而不是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", ""))));

您可以轻松地将 xml 写入字符串:

var myXmlString = doc.ToString();

您也可以使用XDocument.Parse()静态方法实现相同的目标:

var doc = XDocument.Parse("<outputs><output></output> (...) </outputs>");

您也可以使用循环添加内容:

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)));
}
于 2013-02-26T07:40:13.883 回答
2
//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);

并将其作为字符串返回:xmlDoc.OuterXml;

于 2013-02-26T07:49:29.417 回答
1
        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);

并再次创建一个字符串。

        string toString = string.Empty;
        using (StringWriter stringWriter = new StringWriter())
        {
            XmlTextWriter xmlTextWriter = new XmlTextWriter(stringWriter);

            doc.WriteTo(xmlTextWriter);

            toString = stringWriter.ToString();
        }
于 2013-02-26T07:40:33.913 回答
0

这将对您有很大帮助,这是一个关于如何读取和写入 xml 文件的好例子:

http://www.c-sharpcorner.com/UploadFile/mahesh/ReadWriteXMLTutMellli2111282005041517AM/ReadWriteXMLTutMellli21.aspx

关于如何使用 c# 代码创建元素和整个 XML 文件的示例代码:

 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();
    }
于 2013-02-26T07:39:30.703 回答