Click here to Skip to main content
15,885,032 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
Hello friends,
I have to get the data of a specific node from a deserialised XML object.
Below is the deserialization code snippet:
C#
//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


Here is the XML snippet:
</<pre lang="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">/EBAX/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">EBAX/rm</TEAM-MEMBER-REF>
                <DATE>2015-03-25</DATE>
            </DOC-REVISION>
        </DOC-REVISIONS>
    </ADMIN-DATA>
    <PROJECT-DATA>
    <PROJECT-DATA>
</ABC>


Below is the class structure of ABC Class:
C#
public partial class ABC: object, System.ComponentModel.INotifyPropertyChanged {
        
        /// <remarks/>
        public NMTOKENSTRING CATEGORY;
        
        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("ADMIN-DATA")]
        public ADMINDATA ADMINDATA;
        
        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("PROJECT-DATA")]
        public PROJECTDATA PROJECTDATA;
               
        /// <remarks/>
        [System.Xml.Serialization.XmlArrayItemAttribute(IsNullable=false)]
        public SDG[] SDGS;
        
        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string S;
        
        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string T;
        
        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
        
        protected void RaisePropertyChanged(string propertyName) {
            System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
            if ((propertyChanged != null)) {
                propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
            }
        }
    }

 public partial class ADMINDATA : object, System.ComponentModel.INotifyPropertyChanged {
        
        /// <remarks/>
        public LENUM LANGUAGE;
        
        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("USED-LANGUAGES")]
        public MULTILANGUAGEPLAINTEXT USEDLANGUAGES;
        
        /// <remarks/>
        [System.Xml.Serialization.XmlArrayAttribute("COMPANY-DOC-INFOS")]
        [System.Xml.Serialization.XmlArrayItemAttribute(IsNullable=false)]
        public COMPANYDOCINFO[] COMPANYDOCINFOS;
        
        /// <remarks/>
        [System.Xml.Serialization.XmlArrayAttribute("DOC-REVISIONS")]
        [System.Xml.Serialization.XmlArrayItemAttribute(IsNullable=false)]
        public DOCREVISION[] DOCREVISIONS;
        
        /// <remarks/>
        [System.Xml.Serialization.XmlArrayItemAttribute(IsNullable=false)]
        public SDG[] SDGS;
        
        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string S;
        
        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string T;
        
        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
        
        protected void RaisePropertyChanged(string propertyName) {
            System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
            if ((propertyChanged != null)) {
                propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
            }
        }
    }


Now I need to extract the "ADMIN-DATA" data from the XmlData object.
Please suggest how to do that.
Thanks
Posted
Updated 29-May-15 1:23am
v4
Comments
Sinisa Hajnal 29-May-15 7:23am    
It would be nice if you tried something and showed that code and which part of it doesn't work. As-is, you're asking use to do your work...for free.

Try: xmldocument.SelectNodes(XPath_expression), you get nodeCollection...or SelectSingleNode if that is all you need.
[no name] 1-Jun-15 1:09am    
@Sinisa: First of all this is a public forum for everyone who needs any help for free. I posted this question here just to get some immediate ideas or suggestion from someone who had already worked on situation like this, so that some precious time can be saved. Secondly, your response clearly shows that neither you have read the question properly nor u have understood what i had asked. I have clearly asked for suggestions/guidance, not someone to work for me. Also the suggestion provided by u is for getting the selected nodes from xmldocument whereas my source is an object (XmlData). So both are different cases. I can get the data from the object itself but i wanted to know if any smart solution is there.

Thanks anyways for responding.
Sinisa Hajnal 1-Jun-15 2:20am    
Maybe I'm stupid, but if your deserialization passed without error and you want to extract the data from the object (class ABC) why can't you just access it as a property? Is it the problem of knowing which array element to access?

If so, how would you know which node to access in XML? - lets say you need revision number...how would you know which one to take? There are two in your example.

As for my comment: you did indeed use word suggest, but you didn't show any real effort on your part (effort meaning some code you tried to do something and maybe failed or got strange behaviour).
I will not discuss free advice vs free work.
Maciej Los 29-May-15 16:57pm    
I doubt that deserialization completes without error....
[no name] 1-Jun-15 1:03am    
@Maclej: The deserialization code indeed is working without any errors. Thanks anyways for your helpful comment.

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