将对象序列化为XML,仅使用引用ID

本文关键字:引用 ID 对象 序列化 XML | 更新日期: 2023-09-27 18:05:29

我定义了一个包含组件的对象。每个组件都可以包含其他组件作为子组件。像这样:

[Serializable]
public class Thing
{
    private String name;
    private List<MyComponent> components;
    //...
}
[Serializable]
public class MyComponent
{
    private String name;
    private List<MyComponent> subcomponents;
    private String componentID;
    //...
}

序列化现在非常简单:

 Thing t;
 //...lots of stuff happens...
 XmlSerializer x = new XmlSerializer(t.GetType());
 x.Serialize(fstream, t);

当我将一个类型为Thing的对象序列化为XML时,得到的结果如下所示:

<Thing>
  <ID>TH_001<ID>
  <Name>My Test Thing</Name>
  <Components>
     <MyComponent>
        <ComponentID>CMP_001</ComponentID>
            <Name>Foo</Name>
        </MyComponent>        
        <MyComponent>
            <ComponentID>CMP_002</ComponentID>
            <Name>Bar</Name>
            <Subcomponents>
                <MyComponent>
                    <ComponentID>CMP_001</ComponentID>
                     <Name>Foo</Name>
                <MyComponent>
            </Subcomponents>
        </MyComponent>
        <MyComponent>
            <ComponentID>CMP_003</ComponentID>
            <Name>Blergle</Name>
        </MyComponent>   
        <MyComponent>
           <ID>CMP_004</ID>
           <Name>MasterComponent</Name>
          <SubComponents>
                 <MyComponent>
                   <ComponentID>CMP_002</ComponentID>
                   <Name>Bar</Name>
                   <Subcomponents>
                      <MyComponent>
                         <ComponentID>CMP_001</ComponentID>
                         <Name>Foo</Name>
                      <MyComponent>
                    </Subcomponents>
                  </MyComponent>
                  <MyComponent>
                     <ComponentID>CMP_003</ComponentID>
                     <Name>Blergle</Name>
                  </MyComponent>  
         </SubComponents>
      </MyComponent>
   </Components>
 </Thing>

正如你所看到的,有很多冗余。MasterComponent包含所有其他组件的完整定义(还有相当多的其他字段,为了简洁起见,我省略了它们)。我希望看到像这样序列化它:

<Thing>
  <ID>TH_001<ID>
  <Name>My Test Thing</Name>
  <Components>
        <MyComponent>
            <ComponentID>CMP_001</ComponentID>
            <Name>Foo</Name>
        </MyComponent>        
        <MyComponent>
            <ComponentID>CMP_002</ComponentID>
            <Name>Bar</Name>
            <Subcomponents>
                <MyComponent ID="CMP_001" />
            </Subcomponents>
        </MyComponent>
        <MyComponent>
            <ComponentID>CMP_003</ComponentID>
            <Name>Blergle</Name>
        </MyComponent>   
        <MyComponent>
           <ID>CMP_004</ID>
           <Name>MasterComponent</Name>
          <SubComponents>
                 <MyComponent ID="CMP_002" />
                 <MyComponent ID="CMP_003" />
          </SubComponents>
      </MyComponent>
   </Components>
 </Thing>

在这种情况下,组件只被完全定义一次,然后通过ID引用它。我不太确定如何最好地处理这个问题,或者是否有更好的方法。

将对象序列化为XML,仅使用引用ID

查看XML序列化属性。它们提供了一系列控制输出的选项。例如,如果您想将类属性序列化为属性,您可以添加一个XmlAttribute:

[XmlAttribute(AttributeName="id")]
private String componentID;