I have a lot of XML files with the same structure. Many of them working OK, but for some XmlSerializer gives me an error but when I put the document in xml validator - it says that document is correct.
Deserialization code:
var document = serializer.Deserialize(File.OpenRead(file));
Error:
System.InvalidOperationException: There is an error in XML document (504, 8). ---> System.Xml.XmlException: Unexpected node type Element. ReadElementString method can only be called on elements with simple or empty content. Line 504, position 8.
at System.Xml.XmlReader.ReadElementString()
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderPatentdocument.Read33_Claimtext(Boolean isNullable, Boolean checkType)
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderPatentdocument.Read34_Claim(Boolean isNullable, Boolean checkType)
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderPatentdocument.Read35_Claims(Boolean isNullable, Boolean checkType)
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderPatentdocument.Read43_Patentdocument(Boolean isNullable, Boolean checkType)
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderPatentdocument.Read44_patentdocument()
--- End of inner exception stack trace ---
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
at System.Xml.Serialization.XmlSerializer.Deserialize(Stream stream)
The part of the document where it gives the error:
<text>12. Führungsschiene nach einem der Ansprüche 2 bis 11, dadurch gekennzeichnet, daß in den beiden Nutwänden (<b>11<i>a</i>, 11</b><i>a′)</i> einander gegenüberliegende Bohrungen (<b>14</b><i>a</i>, <b>14</b><i>a</i>′) vorgesehen sind, von denen die eine Bohrung (<b>14</b><i>a</i>′) durch das Einsatzteil (<b>15</b><i>a)</i> verschlossen ist.</text>
I suppose it is because of inline html tags inside because it complains about this line on position of i tag
<b>11<i>a</i>, 11</b>
But for example this xml is correct according to XmlSerializer and it is possible to deserialize it:
<text>9. Führungsschiene nach Anspruch 8, dadurch gekennzeichnet, daß der Ansatz (<b>20</b>) die Zuführfläche (<b>25</b>) aufweist.</text>
So my question why xml validator says that the document is valid and XmlSerializer cannot deserialize it? Is it possible to have a workaround without changing the document?
You're right when you point at the inner HTML tags.
Your XML is not valid because you have tags inside a simple (text) element. XmlSerializer doesn't understand and throws an error.
If you have generated the XML files, you have to escape the data inside the simple elements beforehand :
with HTML Encode
Or by encapsulating it in a CDATA tag (<![CDATA[...]]>)
Try serializing the instance that is causing you problems. Then you can compare the output of the serialization with the contents of the file you are attempting to deserialize. The difference between the two XML strings will show you where the problem is.
Here is a quick function to serialize an instance of a class to XML:
public static string Serialize<T>(T entity)
{
if (entity == null)
return String.Empty;
try
{
XmlSerializer XS = new XmlSerializer(typeof(T));
System.IO.StringWriter SW = new System.IO.StringWriter();
XS.Serialize(SW, entity);
return SW.ToString();
}
catch (Exception e)
{
Logging.Log(Severity.Error, "Unable to serialize entity", e);
return String.Empty;
}
}
If you haven't tried it yet, I would suggest the software BeyondCompare to easily see the difference between the two files.
Suppose we have the following class:
public class Foo
{
//[XmlIgnore]
public string Text { get; set; }
}
And xml of the following form:
<Foo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<text>12. Führungsschiene nach einem der Ansprüche 2 bis 11, dadurch gekennzeichnet, daß in den beiden Nutwänden (<b>11<i>a</i>, 11</b><i>a′)</i> einander gegenüberliegende Bohrungen (<b>14</b><i>a</i>, <b>14</b><i>a</i>′) vorgesehen sind, von denen die eine Bohrung (<b>14</b><i>a</i>′) durch das Einsatzteil (<b>15</b><i>a)</i> verschlossen ist.</text>
</Foo>
Then we can deserialize the data as follows.
var xs = new XmlSerializer(typeof(Foo));
xs.UnknownElement += Xs_UnknownElement;
Foo foo;
using (var fs = new FileStream("test.txt", FileMode.Open))
{
foo = (Foo)xs.Deserialize(fs);
}
Subscribe XmlSerializer to UnknownElement event.
In the event handler manually set our property to the data.
private static void Xs_UnknownElement(object sender, XmlElementEventArgs e)
{
var foo = (Foo)e.ObjectBeingDeserialized;
foo.Text = e.Element.InnerXml;
}
Please note that the property name should not match the xml node name (case sensitive). Only in this case the event is triggered. If the names match, use the XmlIgnore attribute.
Related
I am downloading some XML content from the Adobe Connect API. I am loading the content into a XDocument and reading through all of the sco elements to save them to the database. However, one of the calls to the API contains an invalid character that gives the exception:
System.Xml.XmlException: '', hexadecimal value 0x0B, is an invalid character. Line 2, position 6495.
at System.Xml.XmlTextReaderImpl.Throw(Exception e)
at System.Xml.XmlTextReaderImpl.Throw(String res, String[] args)
at System.Xml.XmlTextReaderImpl.ParseText(Int32& startPos, Int32& endPos, Int32& outOrChars)
at System.Xml.XmlTextReaderImpl.ParseText()
at System.Xml.XmlTextReaderImpl.ParseElementContent()
at System.Xml.XmlTextReaderImpl.Read()
at System.Xml.Linq.XContainer.ReadContentFrom(XmlReader r)
at System.Xml.Linq.XContainer.ReadContentFrom(XmlReader r, LoadOptions o)
at System.Xml.Linq.XDocument.Load(XmlReader reader, LoadOptions options)
at System.Xml.Linq.XDocument.Load(XmlReader reader)
at ACRS.DataRefresherApp.Program.GetFolderContents(Folder parentFolder, AcrsDbContext db) in xxx:line 164
Here is a sample of the XML coming from the Adobe Connect API. Note: this example does not contain an invalid character.
<?xml version="1.0"?>
<results>
<status code="ok"/>
<scos>
<sco is-folder="1" duration="" display-seq="0" icon="folder" type="folder" folder-id="xx" source-sco-id="" sco-id="xx">
<name>Shared Templates</name>
<url-path>/f1101964883/</url-path>
<date-created>2010-09-16T15:21:15.993+10:00</date-created>
<date-modified>2013-12-11T22:31:05.130+11:00</date-modified>
<is-seminar>false</is-seminar>
</sco>
.....
</scos>
</results>
Here is the code I am using to read/load the XML data.
Stream responseStream = response.GetResponseStream();
XmlReader xmlReader = XmlReader.Create(responseStream, new XmlReaderSettings() { CheckCharacters = false });
var xmlResponse = XDocument.Load(xmlReader);
var folders = xmlResponse.Elements("results").Elements("scos").Elements("sco").ToList();
The exception occurs when the XDocument attempts to load the data from the xmlReader.
var xmlResponse = XDocument.Load(xmlReader);
I realise that I do not need to use the XmlReader and can load the XDocument directrly from the stream. However, I have included the XmlReader in response to this blog post by Paul Selles.
I have already read this thread:
How to prevent System.Xml.XmlException: Invalid character in the given encoding
However, this does not fix my problem. Apparently, XML standards cause the reader to default to the declared document encoding once the document is being read. In the case of my document where no declaration is being made, it should default to UTF-8. See this answer.
I'm working on an app an i'm saving the data in an XML format but i'm having issues with getting the format correct.
the output looks like this
<GasInfoEntries>
<Gallons>123</Gallons>
<Price>456</Price>
</GasInfoEntries><GasInfoEntries>
<Gallons>123</Gallons>
<Price>456</Price>
</GasInfoEntries>
im using this to write it
List<GasInfoEntries> data = new List<GasInfoEntries>();
data.Add(new GasInfoEntries() { Gallons = TxtBoxGas.Text, Price = TxtBoxPrice.Text });
xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement("GasInfoEntries");
xmlWriter.WriteStartElement("Gallons", "");
xmlWriter.WriteString(TxtBoxGas.Text);
xmlWriter.WriteEndElement();
xmlWriter.WriteStartElement("Price", "");
xmlWriter.WriteString(TxtBoxPrice.Text);
xmlWriter.WriteEndElement();
xmlWriter.WriteEndDocument();
xmlWriter.Flush();
//do i need?
xmlWriter.Close();
i'm getting this error
- $exception
{System.InvalidOperationException: There is an error in XML document (1, 2). ---> System.InvalidOperationException: <GasInfoEntries xmlns=''> was not expected.
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderList1.Read3_ArrayOfGasInfoEntries()
--- End of inner exception stack trace ---
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, Object events)
at System.Xml.Serialization.XmlSerializer.Deserialize(Stream stream)
at LP_Buddy.MainPage.btnShow_Click(Object sender, RoutedEventArgs e)}
System.Exception {System.InvalidOperationException}
any ideas?
thanks
You are missing the root XML element which is required in XML document. Just add it like this:
<GasRoot>
<GasInfoEntries>
<Gallons>123</Gallons>
<Price>456</Price>
</GasInfoEntries>
<GasInfoEntries>
<Gallons>123</Gallons>
<Price>456</Price>
</GasInfoEntries>
</GasRoot>
I appreciate your help.
I have created a web service to receive an XML file, so I followed the below approach then I published it and it worked fine for me :
....
XmlDocument xmldoc = new XmlDocument();
try
{
if (HttpContext.Current.Request.InputStream != null)
{
StreamReader stream = new StreamReader(HttpContext.Current.Request.InputStream);
string xmls = stream.ReadToEnd();
xmldoc.LoadXml(xmls);
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
}
}
catch (Exception ex)
{
logger.Log(NLog.LogLevel.Error, ex.Message + ex.StackTrace);
}
...
knowing that my XML structure is:
<reports uis="5521452542">
<attribute1>val1</attribute1>
...
</reports>
but after testing by some friends, that called my web service from the Lunix platform I received in the Log file error the below message error; knowing that their XML file is validated.
Just to let you know; that their XML file did not contains the declaration of:
<?xml version="1.0" encoding="UTF-8"?>
Can this provide the error or NOT ?
2014-04-03 03:56:53.7408|Error|Root element is missing.
at System.Xml.XmlTextReaderImpl.Throw(Exception e)
at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
at System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace)
at System.Xml.XmlDocument.Load(XmlReader reader)
at System.Xml.XmlDocument.LoadXml(String xml)
at WebService.Service1.GetInfoService() in
D:\yassine\Mobily\Log\WebService\WebService\WebService\Service1.asmx.cs:line 56
2014-04-03 03:56:53.8032|Error|Root element is missing. at System.Xml.XmlTextReaderImpl.Throw(Exception e)
at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
at System.Xml.Linq.XDocument.Load(XmlReader reader, LoadOptions options)
at System.Xml.Linq.XDocument.Parse(String text, LoadOptions options)
at WebService.Service1.GetInfoService() in
D:\yassine\Mobily\Log\WebService\WebService\WebService\Service1.asmx.cs:line 71
Can you please help me to find the exact error please ?
Thank you
The exception is saying exactly whats wrong, you are receiving an invalid xml that has no root element. Ask your friends to send you the raw xml by mail so you could see what they're sending you.
You can you Altova XmlSpy to verify that the xml is valid.
A very basic but valid xml should be:
<root>
<child></child
</root>
For reasons I wont' go into here, I can't use the object type as a parameter into my webapi controller. Therefore, I need to figure out a way to deserialize the xml object into my c# object using XmlDocument, or something similar.
Here's what I have so far:
public void Post(HttpRequestMessage request)
{
var xmlDoc = new XmlDocument();
xmlDoc.Load(request.Content.ReadAsStreamAsync().Result);
using (XmlReader xmlReader = new XmlNodeReader(xmlDoc))
{
Object obj = new XmlSerializer(typeof(myObject)).Deserialize(xmlReader);
myObject scp = (myObject)obj;
}
}
unfortunately that's throwing errors. Can anyone provide some suggestion as to how I can deserialize my xml into my object?
tia
Edit: here's the xml I'm trying to deserialize:
<Student>
<studentid>1234</studentid>
<command>post</command>
<posttype>charge</posttype>
<transaction_description>This is a test post to the web api</transaction_description>
<payment_type>CC</payment_type>
<term_code>2013SPRING</term_code>
<amount>432.75</amount>
</Student>
and here's the error I'm getting:
System.InvalidOperationException: was not expected.
Generated: Wed, 19 Mar 2014 20:18:58 GMT
System.InvalidOperationException: There is an error in XML document
(1, 2). ---> System.InvalidOperationException: was
not expected. at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderStudentChargePost.Read3_StudentChargePost()
--- End of inner exception stack trace --- at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader
xmlReader, String encodingStyle, XmlDeserializationEvents events)
at System.Xml.Serialization.XmlSerializer.Deserialize(Stream stream)
at
CashNetSSO.Controllers.API.StudentInformationPostController.Post(HttpRequestMessage
request) in
C:\Projects\CashNetSSO\Development\CashNetSSO\CashNetSSO\Controllers\API\StudentInformationPostController.cs:line
23 at lambda_method(Closure , Object , Object[] ) at
System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c_DisplayClassf.b_9(Object
instance, Object[] methodParameters) at
System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object
instance, Object[] arguments) at
System.Web.Http.Controllers.ReflectedHttpActionDescriptor.<>c_DisplayClass5.b_4()
at System.Threading.Tasks.TaskHelpers.RunSynchronously[TResult](Func`1
func, CancellationToken cancellationToken)
If you are already reading the content as a stream you can do the following:
myObject scp = null;
XmlSerializer serializer = new XmlSerializer(typeof(myObject);
using (Stream stream = request.Content.ReadAsStreamAsync().Result)
{
scp = serializer.Deserialize(stream);
}
Edit:
The reason you are getting the error is because the XmlSerializer is expecting an xml declaration tag. If your xml does not contain this you can define the root attribute like below:
XmlSerializer serializer = new XmlSerializer(typeof(myObject), new XmlRootAttribute("Student"));
XmlDocument oXmlDoc = new XmlDocument();
try
{
oXmlDoc.Load(filePath);
}
catch (Exception ex)
{
// Log Error Here
try
{
Encoding enc = Encoding.GetEncoding("iso-8859-1");
StreamReader sr = new StreamReader(filePath, enc);
String response = sr.ReadToEnd();
oXmlDoc.LoadXml(response);
}
catch (Exception innerException)
{
// Log Error Here
return false;
}
}
I got xml file from third party which also include the Document Type Definition file after xml declaration.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE SoccerMatchPlus SYSTEM "SoccerMatchPlus.dtd">
<SoccerMatchPlus matchid="33226">
<Booking id="13642055" time="47">
<Player id="370927">
<Name firstName="Lasse" initials="L" lastName="Nielsen">L Nielsen</Name>
</Player>
<Team id="26415" name="AæB" homeOrAway="Home"/>
</Booking>
</SoccerMatchPlus>
If I parse the file with Invalid character in the given encoding. Line 102, position 56. If I catch the exception and retry to parse the file then I got another issue, file parses but
I got the error Could not find file 'C:\Windows\system32\SoccerMatchPlus.dtd'.
Document Type Definition file named SoccerMatchPlus.dtd is added before the root element by third party.
In the case of Load method the parser loads the file from the location where xml file also exists.
I put the SoccerMatchPlus.dtd in other location where xml file resides, can I load that SoccerMatchPlus.dtd file from the specified location at runtime or can you tell me the better way to load the xml file which contains the invalid characters data?
Use the XmlResolver property of XmlDocument class to disable DTD processing.
XmlDocument oXmlDoc = new XmlDocument();
oXmlDoc.XmlResolver = null;