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

时间:2014-05-27 02:07:26

标签: c# xml serialization

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

[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引用。我不太确定如何最好地接近这一点,或者是否有更好的方法。

1 个答案:

答案 0 :(得分:0)

查看XML serialization attributes。它们提供了一系列控制输出的选项。例如,如果要将类属性序列化为属性,则只需添加XmlAttribute:

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