Niranjan Thangaiya
Niranjan Thangaiya

Reputation: 515

How do I read an XML Document object in C#?

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"));      
             }
    }
   }

Upvotes: 3

Views: 3588

Answers (3)

Fredrik Hedblad
Fredrik Hedblad

Reputation: 84656

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);

Upvotes: 1

Christian Tang
Christian Tang

Reputation: 1450

Have you tried looking at LINQ to XML?

Upvotes: 0

Nope
Nope

Reputation: 22339

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.

Upvotes: 3

Related Questions