tags:

views:

139

answers:

3

In a visual C# project i want to pass a XML Document Object to a method. In that method i should Read the values which i stored inside the XML Document object. Without creating an XML File.

Thanks for the Reply guys i finally got my piece of code.

    //use following code when assign values to XMlDocument
         XMLOBJECT()
             {
                XmlDocument xmlEmployee = new XmlDocument();
                XmlElement xmlRoot =  xmlEmployee.CreateElement("HR");
                XmlElement xmlEmployees = xmlEmployee.CreateElement("Employee");
                xmlEmployees.SetAttribute("Name", "XYZ");
                xmlEmployees.SetAttribute("DOB", "12/12/2010");
                xmlRoot.AppendChild(xmlEmployees);
                xmlEmployee.AppendChild(xmlRoot);
                Employee Emp=new EMployee();
                Emp.retriveXMl(xmlEmployee);
              }

In the above code our XML object is created now we can pass the Xml object.

//Use Following code when assign values to Employee Object
 class employee
   {
    retrivelXMl(XMLDOCUMENT xmlEmployeeobject)
    {
    string NAME;
    int DOB;
            XmlNodeList xmlEmployees = xmlEmployeeobject.SelectNodes("//Employee");
             foreach (XmlElement Employee in xmlEmployees)
             {
             NAME = Employee.GetAttribute("Name"));
             DOB   = int.parse(Employee.GetAttribute("DOB"));      
             }
    }
   }
+2  A: 

You can use a XmlNodeReader to access the elements in your XmlDocument.

Depending on what you want to do with the content of the XmlDocument a XmlNodeReader might not suffice. With the little information you provided I added some generic code showing how to access the XmlDocument using a XmlNodeReader to start with.

If you add more details to your question on what it is you are trying to achieve exactly, we might be able to give you an answer better tailored to your needs.

public void WriteXmlDocument(XmlDocument document)
{
    if (document == null)
    {
        throw new ArgumentNullException("document");
    }

    using (XmlNodeReader nodeReader = new XmlNodeReader(document))
    {
        while (nodeReader.Read())
        {
            Console.WriteLine(nodeReader.Value);
        }
    };
}

-- Edit --

To elaborate a bit on the possibilities of the XmlNodeReader. You can also select specific nodes and process them.

Using, the below you can also access a specific node value.

XmlNode specificNode = document.SelectSingleNode("/NodeName/ChildNodeName");

if (specificNode != null)
{
    XmlNodeReader specificNodeReader = new XmlNodeReader(specificNode);

    while (specificNodeReader.Read())
    {
        Console.WriteLine(specificNodeReader.Value);
    }
}

The examples write those node values out to the console, however, you can change this to write the value to a variable for example. The flexibility is there.

François
Thanks for the Reply guys i finally got my piece of code.
Niranjan Thangaiya
+1  A: 

I like to use XmlTextReader and XmlTextWriter. They are very easy to use.

See this link

EDIT

To use from XmlDocument use

XmlTextReader xmlTextReader = new XmlTextReader(new StringReader(xmlDocument.OuterXml));

And if you want to use validation use

XmlValidatingReader xmlValidatingReader = new XmlValidatingReader(xmlTextReader);
Meleak
Yeah, I looked into the use of those first. Though I couldn't find a way of initializing the `XmlTextWriter` without a reference to an actual Xml file, TextWriter or other and I think the OP does not have an actual Xml File available but just a `XmlDocument` object in memory. I was not able to generate a `XmlTextReader` from an `XmlDocument` object either. I checked the link you posted and in the samples the author is always using physical XML files as far as I could see. I think the OPs issue is he only has access to the `XmlDocument` object and does not want to use an actual XML file.
François
Although XmlNodeReader is a good way to go.
Meleak
Nice way of instantiating the `XmlTextReader`. I haven't thought about validation at all at this point. Nice catch (+1 for that); and yes, you are correct, the `XmlNodeReader` does not seem to support Schema Validation or Document Type Definition (DTD).
François
A: 

Have you tried looking at LINQ to XML?

Christian Tang