Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

how to pass XmlDocument Object to XmlTextWriter

Posted on 2006-11-23
4
Medium Priority
?
424 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
1 Comment
 
LVL 5

Accepted Solution

by:
rameedev earned 2000 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses
Course of the Month7 days, 16 hours left to enroll

824 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