C# 如何在C中创建XML#

C# 如何在C中创建XML#,c#,xml,C#,Xml,我需要创建一个XML并将其作为字符串返回。谁能告诉我如何使用XmlDocument创建以下XML <outputs> <output name="" value="" type=""></output> <output name="" value="" type=""></output> <output name="" value="" type=""></output> </outputs&g

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

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

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

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


我认为您应该考虑使用<代码> xObjs而不是<代码> XMLDC/<代码>:

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()
static方法实现相同的目标:

var doc = XDocument.Parse("<outputs><output></output> (...) </outputs>");
并将其作为字符串返回:
xmldoc.OuterXml

您尝试了什么,为什么需要使用
XmlDocument
?(我非常喜欢
XDocument
)好的,那么请给我看一个XDocument的例子……用于上面的XML。请看这个例子:再一次,你尝试了什么?在提出问题之前,展示你已经付出了一些努力是很重要的。关于C#的XML教程有几十个,可能有几百个。我已经把我的部分工作代码放在了更新部分。如何在上面应用foreach循环,因为出现的次数不得而知…虽然完全有效,但这正是微软第一次发布.NET时希望我们编写XML的方式。谢天谢地,事情从那时起就开始了,由@MarcinJuraszek提供的Linq到XML的答案要高得多。@davidkeavany,因此它应该被认为是一个有效的答案。提供链接,提供代码。没有理由在这里否决投票。
var myXmlString = doc.ToString();
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)));
}
        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();
        }
//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);