AasK
AasK

Reputation: 43

C# XML serialize elements from type object without namespace

i want to serialize the following class as child of another class.

[XmlInclude(typeof(string)), XmlInclude(typeof(XML_Variable))]
public class XMLTagDataSection
{
    [XmlElement("Offset")]
    public int XML_Offset        { get; set; }

    [XmlElement("Type")]
    public EnuDataType XML_type  { get; set; }

    [XmlElement("Value")]
    public object XML_Value      { get; set; }

    [XmlElement("Info")]
    public string XML_Info       { get; set; }

    [XmlElement("NumBytes")]
    public int XML_NumBytes      { get; set; }
}

public class XML_Variable
{
    [XmlElement("Variable", IsNullable = false)]       
    public int   Variable { get; set; }
}

this is my actual output:

<Data>
  <Offset>0</Offset>
  <Type>ASCII</Type>
  <Value xmlns:q1="http://www.w3.org/2001/XMLSchema" d7p1:type>test-string</Value>
  <Info>some text</Info>
  <NumBytes>11</NumBytes>
</Data>
<Data>
  <Offset>11</Offset>
  <Type>ASCII</Type>
  <Value d7p1:type="XML_Variable" xmlns:d7p1="http://www.w3.org/2001/XMLSchema-instance">
  <Variable>0</Variable>
  </Value>
  <Info>a variable</Info>
  <NumBytes>5</NumBytes>

how can i get rid of the namespace of my XML_Value element? to get the following output:

<Data>
  <Offset>0</Offset>
  <Type>ASCII</Type>
  <Value>test-string</Value>
  <Info>some text</Info>
  <NumBytes>11</NumBytes>
</Data>
<Data>
  <Offset>11</Offset>
  <Type>ASCII</Type>
  <Value>
    <Variable>0</Variable>
  </Value>
  <Info>a variable</Info>
  <NumBytes>5</NumBytes>

i use this part of code to serialize the parent element:

XmlSerializerNamespaces NameSpace = new XmlSerializerNamespaces();
NameSpace.Add("", "");

XmlSerializer xmlserializer = new XmlSerializer(typeof(XMLRoot));

FileStream str = new FileStream(fileName, FileMode.Create);

xmlserializer.Serialize(str, Root_Tag, NameSpace);

str.Close();

return true;

Upvotes: 1

Views: 1743

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1064004

how can i get rid of the namespace of my XML_Value element? to get the following output:

Easy: it doesn't have one. The namespace of an element can either be specified with an alias prefix:

<foo:Value ...>

or via the reserved xmlns attribute:

<Value xmlns="...." ...>

What you are referring to is the namespace qualifiers for the additional information that XmlSerializer needs to deserialize that data. It only needs it because the type is object, and being dutiful, it wants to be able to understand that data later - otherwise it could never ever deserialize it. One option, then, is to declare your types correctly:

[XmlElement("Value")]
public XML_Variable XML_Value { get; set; }

which will then not need this additional information.

If none of these are possible, then XmlSerializer is not a good choice for you. Consider using XDocument (LINQ-to-XML) or XmlDocument instead.

The entire point of a serializer is to be able to transform the data in both directions, and that will never be possible for object without additional annotations.

Upvotes: 1

Related Questions