在将对象序列化为XML时删除额外的节点
本文关键字:删除 节点 XML 对象 序列化 | 更新日期: 2023-09-27 18:12:53
我有以下对象模型
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属性。谢谢你的帮助!
我最终做了以下更改。我把它贴在这里供将来参考。谢谢@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; }
}