且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在C#中的XML

更新时间:2022-10-22 21:30:06

  //创建XmlDocument的
的XmlDocument xmlDoc中=新的XmlDocument();

//创建的根元素
XmlNode的outputsElement = xmlDoc.CreateElement(输出);

//创建子元素
的XmlElement元= xmlDoc.CreateElement(输出);

//创建名属性
XmlAttribute nameAtt = xmldoc.CreateAttribute(名称);
Element.Attributes.Append(nameAtt);

//创建值属性
XmlAttribute valueAtt = xmldoc.CreateAttribute(值);
Element.Attributes.Append(valueAtt);

//创建type属性
XmlAttribute typeAtt = xmldoc.CreateAttribute(类型);
Element.Attributes.Append(typeAtt);

//追加子元素为根元素
outputsElement.AppendChild(元);

和它返回的字符串:
xmldoc.OuterXml;


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);

//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;