FLICKER FLICKER - 1 month ago 5
C# Question

Removing extra node in serializing object to XML

I have following object model

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; }
}


I serialize
BIDbObjects
class and get below 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>
........


I need to get rid of
DbColumns
in the XML

<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>
.....


Preferably using XML attributes. Thanks for help!

Answer

I ended up with below changes. I post it here for future references. Thank's @jdweng for help.

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; }
}