2

I have an app where the user upload an unknown XML file for me where I read the content (I take each Element and Attribute name) and he can selects which Attribute or Element make reference to each object property. So the user config how to serialize your XML with our Object. And I save the mapping..

So the problem is which is the best practice to convert this XML file to my Object. Keeping in mind that the XML can contain unknown Attribute or Element values.

For example:

<Students>
  <Student>
    <Id>474</Id>
    <Name>Test</Name>
  </Student>
</Students>

And the object:

public clas Student{
  public string cod{ get; set; }
  public string name{ get; set; }
}

I attach an image of how the user define the correlation between the XML elements or attributes and our model class:

enter image description here

Cristian18
  • 220
  • 3
  • 12
  • Is this conceptual or have you started writing code? Will your user define the correlation between the XML elements and your model class? You can use something like `XmlTextReader` to "walk" the XML structure and inspect its elements and attributes. – Glenn Ferrie Jul 19 '19 at 15:03
  • One more thing, "serialization/deserialization" is when you take a known type and you can convert it from an in-memory object to text and then back to an in-memory object. it mainly for storage and transfer of application state. I think your title should be something like, "How do I inspect an unknown XML file to get its elements" – Glenn Ferrie Jul 19 '19 at 15:06
  • more info: https://stackoverflow.com/questions/10150785/using-xmltextreader – Glenn Ferrie Jul 19 '19 at 15:07
  • Thank you for the answer, I've started writing code.. yes, the user define the correlation. I will check `XmlTextReader`. I need to convert any XML to my object class. But the problem is that sometimes the XML can contain Element or Attribute values.. – Cristian18 Jul 19 '19 at 15:12

1 Answers1

1

In the case of using the XmlSerializer, you can make a mapping by using the XmlAttributeOverrides.

string codMapping = "Id"; // get from user
string nameMapping = "Name"; // get from user


var overrides = new XmlAttributeOverrides();

var attrCod = new XmlAttributes();
var attrName = new XmlAttributes();

attrCod.XmlElements.Add(new XmlElementAttribute(codMapping));            
attrName.XmlElements.Add(new XmlElementAttribute(nameMapping));

overrides.Add(typeof(Student), nameof(Student.cod), attrCod);
overrides.Add(typeof(Student), nameof(Student.name), attrName);


var xs = new XmlSerializer(
    typeof(List<Student>), overrides, null, new XmlRootAttribute("Students"), null);
List<Student> students;

using (var fs = new FileStream("test.xml", FileMode.Open))
{
    students = (List<Student>)xs.Deserialize(fs);
}

foreach (var s in students)
    Console.WriteLine(s.cod + " " + s.name);
Alexander Petrov
  • 13,457
  • 2
  • 20
  • 49
  • Great! It can be the solution..But I have a question, Is it possible to perform a resolving for member? like mapping resolver.. – Cristian18 Jul 22 '19 at 13:08