删除序列化对象中的额外节点为XML

时间:2016-10-13 16:30:10

标签: c# xml serialization

我有以下对象模型

public class BIDbObject
{
    // some methods and properties here 

    public BIDbColumns DbColumns { get; set; }
}

[XmlRoot("root")]
public class BIDbObjects
{
    // some methods and properties here 

    [XmlArray("dbobjects")]
    [XmlArrayItem("dbobject")]
    public List<BIDbObject> DbObjects { get; set; }
}

public class BIDbColumn
{
    // some methods and properties here 

    [XmlAttribute("name")]
    public string ColumnName { get; set; }
}

public class BIDbColumns
{
    // some methods and properties here 

    [XmlArray("columns")]
    [XmlArrayItem("column")]
    public List<BIDbColumn> DbColumns { get; set; }
}

我序列化BIDbObjects类并获得XML

<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <dbobjects>
    <dbobject database="Crius" schema="dbo" name="rptAgedPhase3ColLoginReceived" type="T">
      <DbColumns>
        <columns>
          <column name="LoanNum" />
          <column name="BACLOAN_ID" />
          <column name="Unfunded" />
          <column name="RoutingType" />
          <column name="DivAbbrev" />
          <column name="BranchNum" />
          <column name="BranchSatelliteNum" />
          <column name="ClosedCd" />
          <column name="AS400PhaseCd" />
          <column name="RgnNum" />
          <column name="ReceivedDt" />
          <column name="TreasBankPortfolioFlagBit" />
          <column name="TreasBankDestType" />
          <column name="TreasuryBankDestTypeDesc" />
        </columns>
      </DbColumns>
    </dbobject>
    ........

我需要摆脱XML

中的DbColumns
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <dbobject database="Crius" schema="dbo" name="rptAgedPhase3ColLoginReceived" type="T">
    <DbColumns>  <---- this one
      <columns>
      .....

最好使用XML属性。谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

我最终得到了以下变化。我在这里发布以供将来参考。感谢@jdweng寻求帮助。

public class BIDbObject
{
    // some methods and properties here 

    [XmlElement("columns")]
    public BIDbColumns DbColumns { get; set; }
}

[XmlRoot("root")]
public class BIDbObjects
{
    // some methods and properties here 

    [XmlArray("dbobjects")]
    [XmlArrayItem("dbobject")]
    public List<BIDbObject> DbObjects { get; set; }
}

public class BIDbColumn
{
    // some methods and properties here 

    [XmlAttribute("name")]
    public string ColumnName { get; set; }
}

public class BIDbColumns
{
    // some methods and properties here 

    [XmlElement("column")]
    public List<BIDbColumn> DbColumns { get; set; }
}