Solved

how to pass XmlDocument Object to XmlTextWriter

Posted on 2006-11-23
4
412 Views
Last Modified: 2008-02-01
Hi Experts,
 I have a method which takes XmlDocument object as an argument. Now This XmlDocument Object contains the File Name in which I m interested to write my XML. Now How can I use this object to write into my XML file???

I m using XmlTextWriter, but producing Error.....
For clarity I m writing the Code 4 u..

XmlDocument document = new XmlDocument();
            string filename="Peter.xml";
            document.Load(filename);
                                     // or
            document.Save(filename);  // Not Sure

myclass.mymethod(document);  // call 2 mthod
     ................
   ..................

public void mymethod(XmlDocument doc)    // method definition
{
  XmlTextWriter tw = XmlTextWriter(doc,null);
 tw.WriteStartDocument();
 tw.WriteStartElement(c.GetType().ToString());
 ............................
}

It is giving me error
C:\Serializer\Class1.cs(51): 'System.Xml.XmlTextWriter' denotes a 'class' which is not valid in the given context....

if I try to Change XmlDocument object to String variable, It also produces an error....  Type Error

Note 1: I have to make such a method in which I have to pass an XmlDocument Object and write into the file( plz Don't say Y i want to pass XmlDocument object)

Note2: I m not sure how to use AppendChild, CreateElement .... Tell me ..preferably with any example

Plz anyone who can give me any clue how to do it ?
All I NEED to write the XmlDocument object to use for writing XML file....

Looking forward for a quick Response..

Thanks!
Ahit...
 
0
Comment
Question by:ahit
4 Comments
 
LVL 5

Accepted Solution

by:
rameedev earned 500 total points
ID: 18006437
First of all The line of Code  "XmlTextWriter tw = XmlTextWriter(doc,null);" is wrong...there is no overloaded contructor for XmlTextWriter which takes an xmldocument as an arguement...

If you are interested to retreive the filename inside your xmldocument then you need to retreive it from the Xmldocument. You could do this by calling say SelectSingleNode or SelectNode methods which takes an xpath parameter to the node...

Ex:- if we assume your FileName xmlnode is under Root/Parent then
string sPath = "Root/Parent/FileName";
XmlNode nFileName = nd.SelectSingleNode(sPath);
string sFileName = nFileName .InnerText

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question