Let's say I have 2 XML with the same root, Example:
<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.</description>
</book>
</catalog>
and in second XML file:
<?xml version="1.0"?>
<catalog>
<author id="23">
<author>Gambardella, Matthew</author>
<age>45</age>
</author>
<author id="26">
<author>Ali</author>
<age>65</age>
</author>
</catalog>
When I want to deserialize it using Visual Studio, I will create a class for each XML, copy the XML content and paste special > "paste XML as classes".
Then, I will be getting a lot of code generated for me like below:
// NOTE: Generated code may require at least .NET Framework 4.5 or .NET Core/Standard 2.0.
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class catalog
{
private catalogBook[] bookField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("book")]
public catalogBook[] book
{
get
{
return this.bookField;
}
set
{
this.bookField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class catalogBook
{
private string authorField;
private string titleField;
private string genreField;
private decimal priceField;
private System.DateTime publish_dateField;
private string descriptionField;
private string idField;
/// <remarks/>
public string author
{
get
{
return this.authorField;
}
set
{
this.authorField = value;
}
}
/// <remarks/>
public string title
{
get
{
return this.titleField;
}
set
{
this.titleField = value;
}
}
/// <remarks/>
public string genre
{
get
{
return this.genreField;
}
set
{
this.genreField = value;
}
}
/// <remarks/>
public decimal price
{
get
{
return this.priceField;
}
set
{
this.priceField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(DataType = "date")]
public System.DateTime publish_date
{
get
{
return this.publish_dateField;
}
set
{
this.publish_dateField = value;
}
}
/// <remarks/>
public string description
{
get
{
return this.descriptionField;
}
set
{
this.descriptionField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string id
{
get
{
return this.idField;
}
set
{
this.idField = value;
}
}
}
The problem is when I repeat the same step for the second XML, I will get an error in these lines:
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
The error is Duplicate 'System.SerializableAttribute' attribute
My question is how to do fix this problem when my XML file has the same ROOT tag? In this case, the root tag is catalog