Solved

how to pass XmlDocument Object to XmlTextWriter

Posted on 2006-11-23
4
417 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

623 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