protected void btnWriteXML_Click(object sender, EventArgs e)
    {
        //Create document outline
        const string strFileName = "D:\\WriteXMLExample.xml";
        XmlTextWriter objXmlTW = new XmlTextWriter(strFileName, System.Text.Encoding.UTF8);

        //Sets the indentation for the XML file
        objXmlTW.Formatting = Formatting.Indented;
        objXmlTW.Indentation = 4;

        //Write XML Declatation
        objXmlTW.WriteStartDocument();

        //Write root element
        objXmlTW.WriteStartElement("ct", "ContactDetails", "http://www.deltabis.com/Contact");

        //Write attibute to the root element
        objXmlTW.WriteAttributeString("updateDate", "20021201T14:00");

        //Write contact child element node
        objXmlTW.WriteStartElement("contact");

        //Write empty title attribute
        objXmlTW.WriteAttributeString("title", string.Empty);

        //Write <name> element node
        objXmlTW.WriteStartElement("name");

        //Write <first> element node
        objXmlTW.WriteElementString("first", "Steven");

        //Write <middle> element node
        objXmlTW.WriteElementString("middle", string.Empty);

        //Write <last> element node
        objXmlTW.WriteElementString("last", "Livingstone-Perez");

        //Write Full end of <name> element node
        objXmlTW.WriteFullEndElement();

        //Write full end of <contact> element node
        objXmlTW.WriteFullEndElement();

        objXmlTW.WriteEndElement();

        //end the XMLDocument
        objXmlTW.WriteEndDocument();

        //Write to the file and close;
        objXmlTW.Flush();
        objXmlTW.Close();
    } 


相关文章:

  • 2021-04-07
  • 2021-06-08
  • 2022-12-23
  • 2021-10-07
  • 2022-03-10
  • 2021-05-19
  • 2021-09-03
猜你喜欢
  • 2021-05-28
  • 2021-11-11
  • 2022-02-04
相关资源
相似解决方案