Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9108791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T02:53:17+00:00 2026-06-17T02:53:17+00:00

I’ve got a non standard XML that include a template (default values) and specified

  • 0

I’ve got a non standard XML that include a template (default values) and specified fields. As an example is always welcomed :

<MyClass>
   <ArrayOfSubClass>
      <SubClass>
         <Par1>1</Par1>
         <Par2>2</Par2>
         <Par3>3</Par3>
         <ArrayOfSpecific>
            <Specific>
               <Par1>11</Par1>
            </Specific>
         </ArrayOfSpecific>
      </SubClass>
   </ArrayOfSubClass>
</MyClass>

I want to deserialize this class MyClass into an object. As you can see, MyClass is an collection of classes SubClass which has three parameters and a collection of classes Specific. Classes SubClass and Specific are derived from the same mother class.

What ISpecific I would like to find the default values (ie the values found in SubClass) unless a field is given. What I did so far is implementing a method which by reflection checks if the property of the class Specific has a default value of the property type, and replace it by the property of the SubClass if it is the case. It works very well but I don’t cover all the cases. For example, imagine I have a value of the double property Par1 in the SubClass, say Par1 = 1.234, but I want it to be now 0. The problem is that 0 is the default value of type double, so with my method I would retrieve the value of the property in the SubClass.

The best would have been to deserialize the MyClass object first, then put all properties of classes Specific with the value of the same property in SubClass, and then deserialize again into this existing object which would change only properties that are given in the XML file.

It is quite tricky so and I can imagine that my question is not easy to understand…

[Edit:] To try to be more understandable, here is the result I’d like once deserialized

<MyClass>
   <ArrayOfSubClass>
      <SubClass>
         <Par1>1</Par1>
         <Par2>2</Par2>
         <Par3>3</Par3>
         <ArrayOfSpecific>
            <Specific>
               <Par1>11</Par1>
               <Par2>2</Par2>
               <Par3>3</Par3>
            </Specific>
         </ArrayOfSpecific>
      </SubClass>
   </ArrayOfSubClass>
</MyClass>

It is done by reflection after deserialization, if the property of Specific has a default value, then take the value of the class SubClass. The tricky case is as follow

<MyClass>
   <ArrayOfSubClass>
      <SubClass>
         <Par1>1</Par1>
         <Par2>2</Par2>
         <Par3>3</Par3>
         <ArrayOfSpecific>
            <Specific>
               <Par1>0</Par1> <----- 0 is the default value of a double
            </Specific>
         </ArrayOfSpecific>
      </SubClass>
   </ArrayOfSubClass>
</MyClass>

Presently the result would be

<MyClass>
   <ArrayOfSubClass>
      <SubClass>
         <Par1>1</Par1>
         <Par2>2</Par2>
         <Par3>3</Par3>
         <ArrayOfSpecific>
            <Specific>
               <Par1>1</Par1>   <---- as 0 is the default value of a double, this property is set to the value of the SubClass
               <Par2>2</Par2>
               <Par3>3</Par3>
            </Specific>
         </ArrayOfSpecific>
      </SubClass>
   </ArrayOfSubClass>
</MyClass>

but I want

<MyClass>
   <ArrayOfSubClass>
      <SubClass>
         <Par1>1</Par1>
         <Par2>2</Par2>
         <Par3>3</Par3>
         <ArrayOfSpecific>
            <Specific>
               <Par1>0</Par1>
               <Par2>2</Par2>
               <Par3>3</Par3>
            </Specific>
         </ArrayOfSpecific>
      </SubClass>
   </ArrayOfSubClass>
</MyClass>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T02:53:19+00:00Added an answer on June 17, 2026 at 2:53 am

    Ok, I’ve found a workaround that’s solve my issue. I do the thing in two (in fact three) steps. First I deserialize the XML into my class. Second, I put all properties of the Specific classes to default values that I find in the SubClass by reflection, expect one field (the If of the Specific class). And third, I reload the XML into a DataSet. There, I look at the DataTable named Specific, and for all the properties of my class I look if there is any column of the same name, and if the cell contains a value I put it in my class.

    Ouffffff !!!!
    Not really beauty, but it works.

         DataSet xmlDS = new DataSet();
         xmlDS.ReadXml(filename);
         GetSpecifiedValuesInDataSet(xmlDS);
    
         DataTable table = xmlDS.Tables["Specific"];
    
         foreach(ArrayOfSubClass array in this.Items)
         {
            foreach(SubClass sub in array)
            {
               foreach(Specific specific in sub)
               {
                  Type specificType = specific.GetType();
    
                  DataRow modelRow = null;
                  foreach(DataRow row in table.Rows)
                  {
                     if(row["Par1"].ToString().Equals(specific.Par1.ToString()))
                     {
                        modelRow = row;
                        break;
                     }
                  }
    
                  if(modelRow != null)
                  {
                     foreach(PropertyInfo propSpecific in specificType.GetProperties())
                     {
                        string propertyName = propSpecific .Name;
                        foreach(DataColumn col in table.Columns)
                        {
                           if(col.ColumnName.Equals(propertyName))
                           {
                              if(!string.IsNullOrEmpty(modelRow[propertyName].ToString()))
                              {
                                 object value = Convert.ChangeType(modelRow[propertyName], propSpecific.PropertyType);
                                 propSpecific.SetValue(modelProd, value, null);
                              }
                           }
                        }
                     }
                  }
               }
            }
         }
    

    And Voila

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
In my XML file chapters tag has more chapter tag.i need to display chapters

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.