简体   繁体   中英

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;

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM