简体   繁体   English

C#XML序列化类型对象中没有名称空间的元素

[英]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? 如何摆脱XML_Value元素的名称空间? 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? 如何摆脱XML_Value元素的名称空间? 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: 或通过保留的xmlns属性:

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

What you are referring to is the namespace qualifiers for the additional information that XmlSerializer needs to deserialize that data . 您指的是XmlSerializer 反序列化该数据所需的其他信息的名称空间限定符。 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. 它只需要它是因为类型是object ,并且是尽职调查,它希望以后能够理解该数据-否则它将永远无法反序列化它。 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 . 如果这些都不可行,那么XmlSerializer 并不是您的理想选择 Consider using XDocument (LINQ-to-XML) or XmlDocument instead. 考虑改用XDocument (LINQ-to-XML)或XmlDocument

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. 序列化程序的全部要点是能够在两个方向上转换数据,并且如果没有附加注释, object将永远不可能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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