【问题标题】:Deserialize multiple XML elements in one object反序列化一个对象中的多个 XML 元素
【发布时间】:2020-07-26 23:02:18
【问题描述】:

我正在使用 XmlSeriazlier 反序列化 xml 文件。

var serializer = new XmlSerializer(typeof(T));
using (var reader = document.CreateReader())
   var result = (T)serializer.Deserialize(reader);

Xml 包含 if/else if/else 条件,需要一起存储在一个对象中。

示例 Xml

<Layers>
  <If if="something" >
    <Layer name="something" />
  </If>
  <ElseIf if="anything" >
    <Layer name="anything" />
  </ElseIf>
  <Else>
    <Layer name="nothing" />
  </Else>

  <If if="something" >
    <Layer name="something" />
  </If>
  <ElseIf if="anything" >
    <Layer name="anything" />
  </ElseIf>

  <If if="something" >
    <Layer name="something" />
  </If>
  <Else>
    <Layer name="nothing" />
  </Else>
</Layers>

语法与编程语言相同。我想知道如何将连接的条件存储在一起。

[XmlRoot("Layers")
public class Layers
{
    // TODO: Parse conditions to this list.
    public List<Condition> { get; set; }
}

public class Condition
{
    public If IfCondition { get; set; }
    public List<IfElse> IfElseConditions { get; set; }
    public Else ElseCondition { get; set; }
}

【问题讨论】:

    标签: c# xml serialization xmlreader


    【解决方案1】:

    xml 模式的选择存在一些重大问题,这使得它对于XmlSerializer 来说并不是一个真正的好选择。具体来说,没有名义上的&lt;Condition&gt;(或类似的),这意味着从序列化程序的角度没有任何东西可以对这些条件进行分组。要捕获数据,您需要:

    [XmlRoot("Layers")
    public class Layers
    {
        [XmlElement("If")]
        public List<If> { get; set; } // you may be able to share the inner type here
        [XmlElement("ElseIf")]
        public List<ElseIf> { get; set; }
        [XmlElement("Else")]
        public List<Else> { get; set; }
    }
    

    但现在你不知道哪些 if 元素与哪些 else-if 元素搭配,以及与哪些 else 元素搭配。

    我怀疑您必须手动实现这一点,在节点类型之间更改时添加您自己的逻辑,但这并非易事。坦率地说,如果可能的话,重新考虑 xml 布局可能会更容易!例如:

    <Layers>
      <Condition><!-- some kind of "first match wins" grouping -->
        <Match if="something">
            <Layer name="something" />
        </Match>
        <Match if="anything"><!-- note no need for different if/elseif -->
            <Layer name="anything" />
        </Match>
        <Match> <!-- no "if": acts as wildcard, i.e. "else" -->
            <Layer name="nothing" />
        </Match>
      </Condition>
      <!-- etc -->
    </Layers>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-15
      • 1970-01-01
      • 1970-01-01
      • 2021-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多