none
problem in xml to append in existing file RRS feed

  • Question

  • I AM USING THIS CODE TO MAKE A FILE

     XmlWriterSettings mysetting = new XmlWriterSettings();
                mysetting.Indent = true;
                mysetting.IndentChars = "\t";
                mysetting.NewLineChars = "\r\n";
                XmlWriter xw = XmlWriter.Create(@"D:/a.xml", mysetting);
                xw.WriteStartElement("parent");            
                xw.WriteStartElement("child");
                xw.WriteAttributeString("Name", textBox1.Text);
                xw.WriteAttributeString("address", textBox2.Text);
                xw.WriteAttributeString("phone", textBox4.Text);
                xw.WriteEndElement();
                xw.WriteEndElement();

    AND OUTPUT IS 

    <?xml version="1.0" encoding="utf-8"?>
    <parent>
    <child Name="MYNAME" address="MYADDRESS" phone="MYPHONENUMBER" />
    </parent>

    NOW I WANT TO ADD MORE DATA WITHOUT LOSE OLD DATA OUTPUT LIKE THIS

    <?xml version="1.0" encoding="utf-8"?>
    <parent>
    <child Name="MYNAME" address="MYADDRESS" phone="MYPHONENUMBER" />
    <child Name="MYNAME2" address="MYADDRESS2" phone="MYPHONENUMBER2" />
    <child Name="MYNAME3" address="MYADDRESS3" phone="MYPHONENUMBER3" />
    <child Name="MYNAME4" address="MYADDRESS4" phone="MYPHONENUMBER4" />
    </parent>

    HOW???? PLESE HELP

    Monday, August 26, 2019 8:18 AM

All replies

  • Check this:

     

    var doc = new XmlDocument( );

     

    doc.Load( @"path . . ." );

     

    var mysetting = new XmlWriterSettings

    {

        Indent = true,

        IndentChars = "\t",

        NewLineChars = "\r\n",

        ConformanceLevel = ConformanceLevel.Fragment

    };

     

    using( XmlWriter wr = doc.DocumentElement.CreateNavigator( ).AppendChild( ) )

    {

        using( XmlWriter xw = XmlWriter.Create( wr, mysetting ) )

        {

     

            xw.WriteStartElement( "child" );

            xw.WriteAttributeString( "Name", textBox1.Text );

            xw.WriteAttributeString( "address", textBox2.Text );

            xw.WriteAttributeString( "phone", textBox4.Text );

            xw.WriteEndElement( );

            . . . more child nodes . . .

        }

    }

     

    doc.Save( @"path . . ." );

    Monday, August 26, 2019 8:58 AM