XML Serialize from Object

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
i am creating xml from my entity..
I need the following xml structure for output file
<pre class="prettyprint" style=" <?xml version="1.0" encoding="utf-8" ?>
<ErrorData version="1.0" ErrorFileName="
<ErrorDataItem>
<ErrorLine></ErrorLine>
<ErrorMessage></ErrorMessage>
<TimeStamp></TimeStamp>
</ErrorDataItem>
</ErrorData>[/code]
There may have multple "ErrorDataItem"
The following is my entities
<pre class="prettyprint" style=" [XmlRoot("ErrorData")]
public class ExceptionData
{
private List<ErrorData> errorDataField;

private decimal versionField;

private string errorFileNameField;

public ExceptionData()
{
//this.errorDataField = new List<PLMErrorErrorData>();
this.versionField = ((decimal)(1.0m));
}

[System.Xml.Serialization.XmlArrayAttribute(Order = 0)]
[System.Xml.Serialization.XmlArrayItemAttribute("ErrorDataItem", IsNullable = false)]
public List<ErrorData> ErrorData
{
get
{
return this.errorDataField;
}
set
{
this.errorDataField = value;
}
}

[System.Xml.Serialization.XmlAttribute("version")]
public decimal version
{
get
{
return this.versionField;
}
set
{
this.versionField = value;
}
}

[System.Xml.Serialization.XmlAttribute("ErrorFileName")]
public string ErrorFileName
{
get
{
return this.errorFileNameField;
}
set
{
this.errorFileNameField = value;
}
}
}

public class ErrorData
{
[System.Xml.Serialization.XmlElement(Order = 1)]
public int ErrorLine { get; set; }

[System.Xml.Serialization.XmlElement(Order = 2)]
public string ErrorType { get; set; }

[System.Xml.Serialization.XmlElement(Order = 3)]
public string ErrorMessage { get; set; }

[System.Xml.Serialization.XmlElement(Order = 4)]
public DateTime TimeStamp { get; set; }

}[/code]
<br/>

My object creation code is,
<pre class="prettyprint" style=" List<ErrorData> errorData;
if (errorData == null)
{
errorData = new List<ErrorData>();
}
ErrorData error = new ErrorData();
error.ErrorLine = e.Exception.LineNumber;
error.ErrorMessage = e.Message;
error.TimeStamp = DateTime.Now;
errorData.Add(error);

ExceptionData exceptionData = new ExceptionData();
exceptionData.ErrorData = errorData;
exceptionData.ErrorFileName = xmlFileName;[/code]
Then i serialize the exceptionData using Serialize method. The type of Serializable object is<span style="color:#2b91af ExceptionData . But i got the xml as follows
<pre class="prettyprint" style=" <?xml version="1.0"?>
<ErrorData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" ErrorFileName="D:source.xml" version="1.0
<ErrorData>
<ErrorDataItem>
<ErrorLine>5</ErrorLine>
<ErrorMessage>The Code element has an invalid value according to its data type.</ErrorMessage>
<TimeStamp>2012-08-10T19:32:50.5469914+05:30</TimeStamp>
</ErrorDataItem>
</ErrorData>
</ErrorData>[/code]
<br/>
Why ErrorData is duplicated? Where is did mistake?



<br/>

View the full article
 
Back
Top