开发者

XML serialization of enums

开发者 https://www.devze.com 2023-01-19 10:11 出处:网络
I have problems with serializing enum values. Here is the code: [System.Xml.Serialization.XmlRootAttribute(Namespace = \"\", IsNullable = false)]

I have problems with serializing enum values.

Here is the code:

[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public class REQUEST
{
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string ID;

    [System.Xml.Serialization.XmlAttributeAttribute()]
    public REQUESTTypetype Type;
}

public enum REQUESTTypetype
{
    One,
    Two,
    Three,
    Four,
}

...

REQUEST request = new REQUEST();
request.ID = "1234";
request.Type = REQUESTTypetype.One;

XmlDocument doc = new XmlDocument();
MemoryStream ms = new MemoryStream();
StreamWriter sw = new StreamWriter(ms);
XmlSerializer xs = new XmlSerializer(typeof(REQUEST));
xs.Seriali开发者_如何学JAVAze(sw, request_group);
ms.Position = 0;
doc.Load(ms);
TestWriteXml(doc, @"C:\xml_test.xml");

The result is:

<?xml version="1.0" encoding="utf-8" ?> 
<REQUEST ID="1234" />

Why the enum is not serialized? I use .NET Framework 2.0.

Thank you.


I found what was wrong. For every enum type

[System.Xml.Serialization.XmlAttributeAttribute()]
public REQUESTTypetype Type;

I got this:

[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool TypeSpecified;

And in the code I should do this:

request.Type = REQUESTTypetype.One;
request.TypeSpecified = true;

It works fine now. I should have post them in my question but I did not pay attention to these "specified" members at all. Thanks for your replies.


Do you see the same problem when you set the type to "Two" or "Three"? Is it because "One" is the default value and so can be assumed? It might be some artifact of loading this into an XmlDocument and then saving it with your code that you've not shown (TestWriteXml).

This slightly modified version of your code (I'm writing to a StringBuilder and then ToString'ing it at the end)...

    REQUEST request = new REQUEST();
    request.ID = "1234";
    request.Type = REQUESTTypetype.One;


    StringBuilder sb = new StringBuilder();
    StringWriter sw = new StringWriter(sb);
    XmlSerializer xs = new XmlSerializer(typeof(REQUEST));
    xs.Serialize(sw, request);
    Console.WriteLine(sb.ToString());

... using the EXACT same types as you mention above appears to work fine. I get this at the console...

<?xml version="1.0" encoding="utf-16"?>
<REQUEST xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http:/
/www.w3.org/2001/XMLSchema" ID="1234" Type="One" />

I didn't change the definition of REQUEST or the enum REQUESTTypetype.


You can use the Xml.Serialization.XmlEnum attribute (see here) to decorate the values of the enum.

There is a blog post by Kurt Claeys here that might help as well.


In your actual code, is the member:

  • public
  • read+write (for fields: not readonly; for properties: public get+set)
  • of a public type

?

All 3 must be true. For nested types, every parent type in the nesting must be public.

Additional things that would exclude it:

  • nullable and null
  • is if DefaultValue
  • a ShouldSerialize or Specified returned false
  • it is IxmlSerializable


Try putting [Flags] attribute on the enum.

0

精彩评论

暂无评论...
验证码 换一张
取 消