Get Xml Document from file - CSharp System.Xml

CSharp examples for System.Xml:XML Document

Description

Get Xml Document from file

Demo Code


using System.Xml.Linq;
using System.Linq;
using System.Reflection;
using System.Security.Permissions;
using System.IO;/* w w  w .  j  av  a  2s.co  m*/
using System.Collections;
using System.Xml;
using System.Text;
using System.Collections.Generic;
using System;

public class Main{
        public static XmlNode GetXmlDocument(string fileName)
        {
            XmlDocument document = new XmlDocument();
            XmlTextReader reader = null;
            try
            {
                Stream resource = new FileStream(fileName, FileMode.Open);

                reader = new XmlTextReader(resource);
                document.Load(reader);
            }
            catch (Exception exception)
            {
                //
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }

            if (document.ChildNodes.Count > 1)
            {
                return document.ChildNodes[1];
            }
            else
            {
                return document.ChildNodes[0];
            }
        }
}

Related Tutorials