0

I want to load a XML file with XML serialization. The type now should be an enum type. So the XML looks like this:

<Ressource name="ressource_name"  type= "Integer" >
                ...
</Ressource>

And I wanted to load it into a class like this:

[Serializable]
public enum Res_Type
{
    [XmlEnum(Name = "Integer")]
    Integer,
    [XmlEnum(Name = "Decimal")]
    Decimal,
    [XmlEnum(Name = "Text")]
    Text
}

public class Ressource
{
   [XmlAttribute]
   public string name { set; get; }
   [XmlAttribute]
   public Res_Type type { get; set; }
}

When I search for this topic I only find different ways of solving it, then I need it to. I need to have the XML like shown above, but I have no idea how to load the information in type as an enum.


Update: To test the serialization and the deserialization I am using this code:

Ressource res = new Ressource();
res.name = "ressource_name";
res.type = Res_Type.Integer;

XmlSerializer serializer = new XmlSerializer(res.GetType());
using (StreamWriter writer = new StreamWriter(@"h:\test.xml"))
{
    serializer.Serialize(writer, res);
}
XmlSerializer xmlSerializer = new XmlSerializer(typeof(Ressource));
StringReader stringReader = new StringReader(@"h:\test.xml");
res = (Ressource)xmlSerializer.Deserialize(stringReader);

And I am getting the error: InvalidOperationException

dbc
  • 104,963
  • 20
  • 228
  • 340
DomiDiDongo
  • 133
  • 10
  • What do you get when you try this out? Seems okay from my perspective. – MakePeaceGreatAgain Oct 09 '18 at 12:31
  • 1
    Seems like your current [XML Attributes](https://learn.microsoft.com/en-us/dotnet/standard/serialization/attributes-that-control-xml-serialization) work: if I serialize your class to XML I get ``. See https://dotnetfiddle.net/Muuv4O. So what exactly is your problem? Are you asking how to serialize any object from and to XML? If so see [Serialize an object to XML](https://stackoverflow.com/q/4123590). – dbc Oct 09 '18 at 12:31
  • @dbc Wouldn´t have a simple `XmlSerializer` done the job with a single line? Anyway I agree, I don´t see any problem on OPs code. – MakePeaceGreatAgain Oct 09 '18 at 12:33
  • @HimBromBeere - yes it would, in which case this is a duplicate of [Serialize an object to XML](https://stackoverflow.com/q/4123590). – dbc Oct 09 '18 at 12:34
  • I have more the problem that I have the Xml and I want to generate the class out of it. I am getting an InvalidOperationException. But I am still looking through what you send me, so maybe I will find something there – DomiDiDongo Oct 09 '18 at 12:55
  • I added the code I am using for serialization and deserialization. I don't see any difference to the code you send. But I have no idea why it is not working. I thought all the time, the problem was, that I can not load attributes to enums like that. – DomiDiDongo Oct 09 '18 at 13:10
  • @DomiDiDongo - can you [edit] your question to share the full `ToString()` output of the exception including the exception type, message, traceback and inner exception(s), if any? Often the inner exception explains the real problem. – dbc Oct 09 '18 at 13:22
  • 1
    Oh, you are using `StringReader` but you need to use `StreamReader`. – dbc Oct 09 '18 at 13:23
  • Ok, thank you ^^ now my small solution works :D Now I only have to find the problem in the big project. Thank you. – DomiDiDongo Oct 09 '18 at 13:29

1 Answers1

1

Your problem is that you are using a StringReader rather than a StreamReader:

StringReader stringReader = new StringReader(@"h:\test.xml");

This means that your code is attempting to deserialize the contents of the string literal @"h:\test.xml" itself rather than the file to which it refers. This of course fails because the string h:\test.xml is not even well-formed XML.

Instead you should do:

var fileName = @"h:\test.xml";

// Write the file as before

using (var reader = new StreamReader(fileName))
{
    res = (Ressource)xmlSerializer.Deserialize(reader);
}

Working .Net fiddle here.

dbc
  • 104,963
  • 20
  • 228
  • 340