Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 132,676 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,197 people online right now. Registration is fast and FREE... Join Now!




writing to an xml file

 
Reply to this topicStart new topic

writing to an xml file

hiphop_13
post 13 Sep, 2007 - 12:42 AM
Post #1


New D.I.C Head

*
Joined: 4 Jun, 2007
Posts: 36



Thanked 1 times
My Contributions


Hi

i have this xml file
CODE

<form>
  <name>test</name>
  <controls>
      <control type="textbox" caption="name" order="1"></control>
  </controls>
</form



i used the following code to write the xml file, but it didnt work:( plz help
CODE

XmlTextWriter bankWriter = new XmlTextWriter("C:\\Documents and Settings\\User\\My Documents\\Visual Studio 2005\\Projects\\xml.xml", null);
            try
            {
                bankWriter.Formatting = Formatting.Indented;
                bankWriter.Indentation = 6;
                bankWriter.Namespaces = false;

                bankWriter.WriteStartDocument();

                bankWriter.WriteStartElement("", "form", "");

                bankWriter.WriteStartElement("", "name", "");
                bankWriter.WriteString("test");
                bankWriter.WriteEndElement();

                bankWriter.WriteStartElement("", "controls", "");
                bankWriter.WriteElementString("control",null);
                bankWriter.WriteAttributeString("type", "textbox");
                bankWriter.WriteAttributeString("caption", "name");
                bankWriter.WriteAttributeString("order", "1");
                bankWriter.WriteEndAttribute();
                bankWriter.WriteEndAttribute();
                bankWriter.WriteEndAttribute();
                bankWriter.WriteEndElement();
                bankWriter.WriteEndElement();
                bankWriter.WriteEndElement();
                
                bankWriter.Flush();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: {0}", e.ToString());
            }
            finally
            {
                if (bankWriter != null)
                {
                    bankWriter.Close();
                }

User is offlineProfile CardPM

Go to the top of the page

hammerstein2007
post 13 Sep, 2007 - 02:53 AM
Post #2


New D.I.C Head

*
Joined: 13 Sep, 2007
Posts: 1


My Contributions


You start an element and until you end the element, the attributes are added to that element. Make sure you're matching up the element start/end properly in your code. The code you posted earlier wouldn't compile without some changes.

If you read the exceptions you're getting, look at the resulting output (it does stop at where it was in the file.. ) you can figure out just what is happening.

CODE

XmlTextWriter bankWriter =
                new XmlTextWriter(
                    @"C:\\xml.xml", null );
            try
            {
                bankWriter.Formatting = Formatting.Indented;
                bankWriter.Indentation = 6;
                bankWriter.Namespaces = false;

                bankWriter.WriteStartDocument( );

                bankWriter.WriteStartElement( "", "form", "" );

                bankWriter.WriteStartElement( "", "name", "" );
                bankWriter.WriteString( "test" );
                bankWriter.WriteEndElement( );

                bankWriter.WriteStartElement( "", "controls", "" );
                bankWriter.WriteStartElement( "", "control", "" );
                bankWriter.WriteAttributeString( "type", "textbox" );
                bankWriter.WriteAttributeString( "caption", "name" );
                bankWriter.WriteAttributeString( "order", "1" );
                bankWriter.WriteEndElement( );
                bankWriter.WriteEndElement( );
                bankWriter.WriteEndElement( );

                bankWriter.Flush( );
            }
            catch ( Exception ex )
            {
                Console.WriteLine( "Exception: {0}", ex.ToString( ) );
            }
            finally
            {
                    bankWriter.Close( );
            }
User is offlineProfile CardPM

Go to the top of the page

jp42
post 14 Sep, 2007 - 12:30 PM
Post #3


New D.I.C Head

*
Joined: 21 Aug, 2007
Posts: 38


My Contributions


You might want to have a look at the XmlDocument class :
http://msdn2.microsoft.com/en-us/library/s...mldocument.aspx

I have been working with writing to xml quite a bit lately, and use that class for all my needs. Here is some sample code for you, I was writing an Rss doc... youll get the idea...

CODE

private void CreateNewRssDocument()
        {
            _rssDoc = new XmlDocument();
            XmlDeclaration xmlDec = _rssDoc.CreateXmlDeclaration("1.0", "utf-8", null);
            //create the root(rss) element
            XmlElement rssElement = _rssDoc.CreateElement(_dictionary["rssElement"]);
            //set the attribute and value for the rss element
            rssElement.SetAttribute(_dictionary["rssElementAttr"], _dictionary["rssElementAttrValue"]);
            //append the root
            _rssDoc.AppendChild(rssElement);
            //create the channel element
            XmlElement channelElement = _rssDoc.CreateElement(_dictionary["channelElement"]);
            //append the channel element to the beginning of the rss element children
            rssElement.PrependChild(channelElement);
            //set the static children of the channel element
            //title
            XmlElement titleElement = _rssDoc.CreateElement(_dictionary["titleElement"]);
            channelElement.AppendChild(titleElement);
            titleElement.InnerText = _dictionary["titleElementValue"];
            //link
            XmlElement linkElement = _rssDoc.CreateElement(_dictionary["linkElement"]);
            channelElement.AppendChild(linkElement);
            linkElement.InnerText = _dictionary["linkElementValue"];
            //language
            XmlElement languageElement = _rssDoc.CreateElement(_dictionary["languageElement"]);
            channelElement.AppendChild(languageElement);
            languageElement.InnerText = _dictionary["languageElementValue"];
            //description
            XmlElement descriptionElement = _rssDoc.CreateElement(_dictionary["descriptionElement"]);
            channelElement.AppendChild(descriptionElement);
            descriptionElement.InnerText = _dictionary["descriptionElementValue"];

            //insert the xml declaration before the document element
            _rssDoc.InsertBefore(xmlDec, _rssDoc.DocumentElement);

            //save the doc
            _rssDoc.Save(_docPath);

        }


This post has been edited by jp42: 14 Sep, 2007 - 12:31 PM
User is offlineProfile CardPM

Go to the top of the page

hiphop_13
post 15 Sep, 2007 - 01:31 PM
Post #4


New D.I.C Head

*
Joined: 4 Jun, 2007
Posts: 36



Thanked 1 times
My Contributions


Thanks a lot.
I appreciate your help
smile.gif

Thank you for your help:)
Thanks
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 06:29AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month