如何使用名称空间反序列化xml元素

时间:2014-04-27 03:35:26

标签: xml serialization xml-serialization

我有这个xml:

<Configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Tag>
    <Id>1.2.39</Id>
    <TagName>DescriptionFeatures</TagName>
    <ItemDesignId>2115</ItemDesignId>
    <Childs />
    <FormElement xsi:type="TextBoxElement">
      <Id>DescriptionFeatures</Id>
      <LabelText>Description Features</LabelText>
      <DisplayOrder>0</DisplayOrder>
      <HelpMessage>Enter Description Features text</HelpMessage>
      <Value></Value>
      <FormElementType>Level2</FormElementType>
      <Validators>
        <DataValidator xsi:type="RequiredValidator">
          <ErrorMessage>Description Features is required</ErrorMessage>
          <MinSize>0</MinSize>
          <MaxSize>0</MaxSize>
        </DataValidator>
      </Validators>
      <OldValue />
      <TextFormats />
      <ParaStyles />
    </FormElement>
    <DisplayOrder>455</DisplayOrder>
    <RotationAngle>0</RotationAngle>
</Tag>

我的模特:

标签:

public class TagModel
    {
        [XmlElement("Id")]
        public string Id { get; set; }

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

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

        [XmlArray("Childs")]
        [XmlArrayItem("Tag", typeof(TagModel))]
        public List<TagModel> Childs { get; set; }

        [XmlElement("FormElement", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
        public FormElementModel FormElement { get; set; }

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

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

和FormElement

public class FormElementModel
    {
        [XmlElement("Id")]
        public string Id { get; set; }

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

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

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

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

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

        [XmlElement("FormElementType")]
        public string FormElemenType { get; set; }

        [XmlArray("Validators")]
        [XmlArrayItem("DataValidator", typeof(DataValidatorModel))]
        public List<DataValidatorModel> Validators { get; set; }

        [XmlArray("Items")]
        [XmlArrayItem("ListItem", typeof(ListItemModel))]
        public List<ListItemModel> Items { get; set; }
    }

我有反序列化功能:

public void Deserialize()
    {
      var type = typeof(TagModel);
        var s = new XmlSerializer(type);
        string xml = /* xml content here */
        var reader = new StringReader(xml);
        var o = (TagModel)s.Deserialize(reader);
    }

除了FormElement之外,它返回所有元素。 FormElement始终为空。请我解决。

0 个答案:

没有答案