Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hello friends,
We have a big XML file (>27MB) to be parsed. We had deserialized the XML file into a XmlData object. The data from the XML is perfectly saved in this XmlData object in a proper structure of classes and objects.

Now, the goal here is to get the specific node data from the XmlData based on the Selected Node Path. I believe this can be done using Linq to XML query [Or] XPath.
But, I dont prefer Linq to XML because in this case i have to write specific query for specific node and i may have to hardcode the Specific Node names. This will be tough due to the complexity and imense XML data.

In case of XPath, i think we just need to provide the Selected node path and it will collect the corresponding data under it.
For eg. If i need to get the data under DOC-REVISIONS, then i will just send the Node path and retrieve all the data under it in Obj variable.

var Obj = Xpath (/node/path) [PS: plz ignore the syntax]
And this variable i want to use in my application.

Please can somebody guide me how i can achieve this.

Below mentioned is the code snippet and XML snippet, for your reference:
C#
public void CreateDeserializedXmlObject(string strSrcFilename)
        {
            try
            {
                //Using this object we can access the different classes & objects created from that XML

                XmlSerializer deserializer = new XmlSerializer(typeof(ABC));
                TextReader reader = new StreamReader(strSrcFilename);
                object obj = deserializer.Deserialize(reader);
                XmlData = (ABC)obj; // XmlData will have all the parsed data in form of Objects
                    reader.Close();
            }
            catch (Exception ex)
            {}
        }

XML
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
<ABC xmlns="http://www.ABC.net/schema/ABC/r1.0">
    <CATEGORY>TestReport</CATEGORY>
    <ADMIN-DATA>
        <LANGUAGE>DE</LANGUAGE>
        <USED-LANGUAGES>
            <L-10 xml:space="preserve" L="EN">EN</L-10>
        </USED-LANGUAGES>
        <DOC-REVISIONS>
            <DOC-REVISION>
                <REVISION-LABEL>1.0.0.1</REVISION-LABEL>
                <STATE>under_develpment</STATE>
                <TEAM-MEMBER-REF DEST="TEAM-MEMBER">/EBCG/rm</TEAM-MEMBER-REF>
                <DATE>2015-03-27</DATE>
            </DOC-REVISION>
            <DOC-REVISION>
                <REVISION-LABEL>1.0.0.0</REVISION-LABEL>
                <STATE>under_develpment</STATE>
                <TEAM-MEMBER-REF DEST="TEAM-MEMBER">/XYZ/dro</TEAM-MEMBER-REF>
                <DATE>2015-03-25</DATE>
            </DOC-REVISION>
        </DOC-REVISIONS>
    </ADMIN-DATA>


Thanks.
Posted
Updated 27-May-15 1:54am
v2

You can start by looking at below article

Manipulate XML data with XPath and XmlDocument (C#)[^]
 
Share this answer
 
Comments
[no name] 27-May-15 8:18am    
Thanks Asif for the response. But the problem here is:
The source for parsing should be the XmlData deserialized object as mentioned above, not the xml file directly. I am not sure whether this is achievable by XPath or not. If possible then how?
Please guide.
_Asif_ 27-May-15 8:35am    
You have deserialized Xml but you want to access it as Xml. Xpath/XQuery needs and runs over XmlDocument object, its kind of a prerequisite so you need to preserve your xml somewhere so that you can execute XPath/Xquery over it.

One approach could be to have an attribute "sourceXml" of type XmlDocument in XmlData object. Using this you can execute your xpath queries XmlData.sourceXml.SelectNodes(xpath). This approach has some downside as well as memory will grow big since you now have both deserialized form of Xml and original xml as well.
Hi,

I think you would like to deal with the deserialized object and want to find an object at specified path. Below code snippet might help you in doing this.

C#
class Program
    {
        static void Main(string[] args)
        {
            Student student = new Student { ID = 1, Name = "Abc", Department = new Department { ID = 1, Name = "Xyz" } };

            object result = student.Find("Department", "Name");

            Console.WriteLine(result);
            Console.ReadKey();
        }

        class Student
        {
            public int ID { get; set; }
            public string Name { get; set; }
            public Department Department { get; set; }
        }

        class Department
        {
            public int ID{ get; set; }
            public string Name { get; set; }
        }        
    }

    static class MiscExtensions
    {
        public static object Find(this object o, params string[] keys)
        {
            if (o == null) throw new ArgumentNullException();
            if (!keys.Any()) throw new ArgumentNullException();

            Type t = o.GetType();

            foreach (var property in t.GetProperties())
            {
                if (property.Name.Equals(keys[0], StringComparison.InvariantCultureIgnoreCase))
                {
                    if (keys.Count() > 1)
                        return property.GetValue(o).Find(keys.Skip(1).ToArray());
                    else
                    {
                        return property.GetValue(o);
                    }
                }
            }

            throw new Exception("Path not found");
        }
    }
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900