0

Quite straights forward issue, but still can't figure out... I'm trying to deserialize XML which contains collection of XmlElements. I would like to deserialize the elements (First and second). The most important elements are the ones in the ImportantNode. The third is less important.

The failure is at the Deserialization method.

This is the xml:

<configuration>
  <objects>
        <!-- this is a comment -->  
         <First>
          <param name="Name" type="string">FirstName</param>
          <param name="Type" type="string">FirstType</param>
          <SomeElement>
               <SomeElement1>
                  <param name="Name" type="string">AAA</param>
                  <param name="Name2" type="string">BBB</param>
              </SomeElement1>
          </SomeElement>
          <param name="param1" type="string">1</param>
          <param name="param2" type="int">2</param>
          <param name="param3" type="float">3</param>
          <ImportantNode>
            <ImportantChildNode1>
              <param name="minValue" type="float">1</param>
              <param name="maxValue" type="float">100</param>
            </ImportantChildNode1>
            <ImportantChildNode2>
              <param name="minValue" type="float">1</param>
              <param name="maxValue" type="float">100</param>
            </IR>
        </ImportantNode>
        <elements>
          <element>
              <param name="paramName1" type="bool">true</param>
          </element>
        </elements>
      </First>
      <second>
          <param name="Name" type="string">2</param>
          <param name="Type" type="string">3</param>
          <ImportantNode>
            <ImportantChildNode1>
              <param name="minValue" type="float">2</param>
              <param name="maxValue" type="float">20</param>
            </ImportantChildNode1>
            <ImportantChildNode2>
              <param name="minValue" type="float">3</param>
              <param name="maxValue" type="float">30</param>
            </IR>
          </ImportantNode>
      </second>
      <third>
          <param name="Name" type="string">3</param>
          <param name="Type" type="string">4</param>
          <AAAA>
            <param name="DDD" type="string">XXX</param>
          </AAAA>
          <BBBB>
            <param name="DDD" type="string">ZZZ</param>
          </BBBB>
          <param name="Param1" type="string">6</param>
          <param name="Param2" type="string">301</param>
      </third>
    
    </objects>
</configuration>
  

This is the related classes:

[Serializable]
[XmlRoot(ElementName = "ImportantNode")]
public class ImportantNode
{
    [XmlElement(ElementName = "ImportantChildNode1")]
    public Param ImportantChildNode1Params { get; set; }
    [XmlElement(ElementName = "ImportantChildNode2")]
    public Param ImportantChildNode2Params { get; set; }
}

 public class Param
{
    [XmlAttribute(AttributeName = "name")]
    public string Name { get; set; }
    
    /*
    [XmlAttribute(AttributeName = "type")]
    public string type { get; set; }*/
    [XmlText]
    public string Value { get; set; }
}

[Serializable]
public class DeserializeObject
{
    [XmlArrayItem("param")]
    public List<Param> Params { get; set; }
    [XmlElement (ElementName ="ImportantNode")]
    public ImportantNode ImportantNodeParams { get; set; }
}

And this is the relevant code:

IEnumerable<DeserializeObject> DeserializeObjectsList;
XmlReaderSettings readSettings = new XmlReaderSettings();
readSettings.IgnoreComments = true;

string filePath = "<xml file path>";

//using (XmlReader reader = XmlReader.Create(filePath, readSettings)
XmlReader reader = XmlReader.Create(filePath, readSettings);
XmlDocument filedoc = new XmlDocument();
filedoc.Load(reader/*filePath*/);


//using var fileStream = new FileStream(filePath, FileMode.Open);
XmlNodeList xmlNodeList = filedoc["configuration"].GetElementsByTagName("objects");

if (xmlNodeList == null)
{
    throw new Exception("configuration file problem");
}
DeserializeObject dObject = new DeserializeObject();
XmlSerializer serializer = new XmlSerializer(dObject.GetType());

var objects = xmlNodeList[0].ChildNodes;

foreach (XmlElement object in objects)
{
    var sElement = object.InnerXml;
    //string AAA = sElement.ToString();
    StringReader SR = new StringReader(/*sElement.InnerXml*//*sElement*/object.OuterXml);
    var deserializedObject = serializer.Deserialize(SR);
    dObject = (DeserializeObject)serializer.Deserialize(SR);
}
Guy E
  • 1,775
  • 2
  • 27
  • 55
  • The following may be helpful: https://stackoverflow.com/questions/73638292/create-delete-element-on-xmlnode-c-sharp/73640395#73640395, https://stackoverflow.com/a/68607179/10024425, and https://stackoverflow.com/q/68215415/10024425. – Tu deschizi eu inchid Feb 16 '23 at 15:15
  • If you highlight your XML and press `Ctrl-C`. Then create a new class in VS, then click `Edit` => `Paste Special` => `Paste XML As Classes`, VS will create the classes for you. – Tu deschizi eu inchid Feb 16 '23 at 15:24
  • Of course, you'll have to fix your XML first so that it's correct. For example, the ending "configuration" tag first - should be ``. There are additional issues with the XML that need to be fixed as well. The easiest way to Debug your XML when you've created the classes is to serialize it (ie: write it to file) to see if the output XML matches what you expect. Once you've got the serialization correct, then you can deserialize the XML. – Tu deschizi eu inchid Feb 16 '23 at 15:24
  • @user09938 - not feasible in my case - the generated code is far from being useable – Guy E Feb 16 '23 at 15:54

0 Answers0