EDN Admin
Well-known member
Im getting following error when i try to load XDocument. Here my code
<div style="background-color:white; color:black
<pre> <span style="color:blue private <span style="color:blue void GenerateXML()
{
AppealRoot root = <span style="color:blue new AppealRoot();
root.AppealForm = <span style="color:blue new AppealRootAppealForm();
root.AppealForm.TaxYear = 2011;
root.AppealForm.FirstName = <span style="color:#a31515 "James";
root.AppealForm.LastName = <span style="color:#a31515 "Smith";
MyXmlSerializer<AppealRoot> ser = <span style="color:blue new MyXmlSerializer<AppealRoot>();
<span style="color:blue string xml = ser.Serialize(root);
XDocument xDoc = XDocument.Load(<span style="color:blue new StringReader(xml));
WriteToFile(xml);
}
<span style="color:blue public <span style="color:blue class MyXmlSerializer<T>
{
<span style="color:blue private XmlSerializer _serializer = <span style="color:blue null;
<span style="color:blue public MyXmlSerializer()
{
_serializer = <span style="color:blue new XmlSerializer(<span style="color:blue typeof(T));
}
<span style="color:blue public <span style="color:blue string Serialize(T instance)
{
<span style="color:green //Since .NET strings are always stored in UTF-16, if we use StringWriter or StringBuilder
<span style="color:green //our resulting xml string will have the encoding of UTF-16. One way to get around this is
<span style="color:green //by creating a MemoryStream, creating a StreamWriter, and applying the UTF-8 encoding.
<span style="color:blue using (System.IO.MemoryStream stream = <span style="color:blue new System.IO.MemoryStream())
{
<span style="color:blue using (System.Xml.XmlTextWriter xtWriter = <span style="color:blue new System.Xml.XmlTextWriter(stream, Encoding.UTF8))
{
_serializer.Serialize(xtWriter, instance);
xtWriter.Flush();
stream.Seek(0, System.IO.SeekOrigin.Begin);
<span style="color:blue return Encoding.UTF8.GetString(stream.ToArray());
}
}
}
}
[/code]
<pre lang="x-xml "<?xml version="1.0" encoding="utf-8"?><AppealRoot xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.domain.com/schemas
<AppealForm>
<DateFiled>
<DateValue>2011-04-22</DateValue>
<Day>22</Day>
<Month>4</Month>
<Year>2011</Year>
</DateFiled>
<FirstName>James</FirstName>
<LastName>Smith</LastName>
</AppealForm>
</AppealRoot>"[/code]
<br/>
View the full article
<div style="background-color:white; color:black
<pre> <span style="color:blue private <span style="color:blue void GenerateXML()
{
AppealRoot root = <span style="color:blue new AppealRoot();
root.AppealForm = <span style="color:blue new AppealRootAppealForm();
root.AppealForm.TaxYear = 2011;
root.AppealForm.FirstName = <span style="color:#a31515 "James";
root.AppealForm.LastName = <span style="color:#a31515 "Smith";
MyXmlSerializer<AppealRoot> ser = <span style="color:blue new MyXmlSerializer<AppealRoot>();
<span style="color:blue string xml = ser.Serialize(root);
XDocument xDoc = XDocument.Load(<span style="color:blue new StringReader(xml));
WriteToFile(xml);
}
<span style="color:blue public <span style="color:blue class MyXmlSerializer<T>
{
<span style="color:blue private XmlSerializer _serializer = <span style="color:blue null;
<span style="color:blue public MyXmlSerializer()
{
_serializer = <span style="color:blue new XmlSerializer(<span style="color:blue typeof(T));
}
<span style="color:blue public <span style="color:blue string Serialize(T instance)
{
<span style="color:green //Since .NET strings are always stored in UTF-16, if we use StringWriter or StringBuilder
<span style="color:green //our resulting xml string will have the encoding of UTF-16. One way to get around this is
<span style="color:green //by creating a MemoryStream, creating a StreamWriter, and applying the UTF-8 encoding.
<span style="color:blue using (System.IO.MemoryStream stream = <span style="color:blue new System.IO.MemoryStream())
{
<span style="color:blue using (System.Xml.XmlTextWriter xtWriter = <span style="color:blue new System.Xml.XmlTextWriter(stream, Encoding.UTF8))
{
_serializer.Serialize(xtWriter, instance);
xtWriter.Flush();
stream.Seek(0, System.IO.SeekOrigin.Begin);
<span style="color:blue return Encoding.UTF8.GetString(stream.ToArray());
}
}
}
}
[/code]
<pre lang="x-xml "<?xml version="1.0" encoding="utf-8"?><AppealRoot xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.domain.com/schemas
<AppealForm>
<DateFiled>
<DateValue>2011-04-22</DateValue>
<Day>22</Day>
<Month>4</Month>
<Year>2011</Year>
</DateFiled>
<FirstName>James</FirstName>
<LastName>Smith</LastName>
</AppealForm>
</AppealRoot>"[/code]
<br/>
View the full article