xml deserialization error in namespace or root directory
I have a xml file that was not generated by me and I cannot change it because a similar file is generated each day and I must read it as it is.
it begins thus:
Code:
<?xml version="1.0" encoding="UTF-8" ?>
<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"
xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"
xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2"
xmlns:mcd="urn:acss:ccf:facturacaoelectronica:schema:xsd:Normalizados">
<ext:UBLExtensions>
<ext:UBLExtension>
<ext:ExtensionVersionID>ACSS:CCF:NormalizadosExtension:1.0</ext:ExtensionVersionID>
<ext:ExtensionContent>
<mcd:NormalizadosExtension>
....
The class generated through xsd.exe from a partial Normalizados.xsd schema begins like this:
Code:
Option Strict Off
Option Explicit On
Imports System.Xml.Serialization
'
'This source code was auto-generated by xsd, Version=4.6.81.0.
'
'''<remarks/>
<System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.81.0"), _
System.SerializableAttribute(), _
System.Diagnostics.DebuggerStepThroughAttribute(), _
System.ComponentModel.DesignerCategoryAttribute("code"), _
System.Xml.Serialization.XmlTypeAttribute([Namespace]:="urn:acss:ccf:facturacaoelectronica:schema:xsd:Normalizados"), _
System.Xml.Serialization.XmlRootAttribute("NormalizadosExtension", [Namespace]:="urn:acss:ccf:facturacaoelectronica:schema:xsd:Normalizados", IsNullable:=false)> _
Partial Public Class NormalizadosExtensionType
the code to deserialize is this:
Code:
Public Sub lerxml()
Dim ObjStreamReader As New StreamReader(ficheiroxml, System.Text.Encoding.Default)
Dim norm As New NormalizadosExtensionType()
Dim dmcd As New XmlSerializer(norm.GetType)
norm = dmcd.Deserialize(ObjStreamReader)
...
End Sub
the last line throws an error: An unhandled exception of type 'System.InvalidOperationException' occurred in System.Xml.dll Additional information: There is an error in XML document (5, 2).
this means an error on the xml file when Invoice first appears.
inner exception shows "<Invoice xmlns='urn: oasis:names:specification:ubl:schema:xsd:Invoice-*2'> was not expected."
The class I want to be the root (because I only have the schema for its subclass and don't really need anything outside it), Normalizados, is a subclass of a subclass of a subclass of Invoice ( Invoice > UBLExtension > ExtensionContent > NormalizadosExtension )
What's wrong with the code? Thanks
Re: xml deserialization error in namespace or root directory
Checking through MSDN docs, you need to restore the object's state when performing deserialization.
VB.NET Code:
norm = CType(dcmd.Deserialize(reader), NormalizadosExtensionType)
MSDN XmlSerializer.Deserialize()
Re: xml deserialization error in namespace or root directory
Quote:
Originally Posted by
KGComputers
Checking through MSDN docs, you need to restore the object's state when performing deserialization.
VB.NET Code:
norm = CType(dcmd.Deserialize(reader), NormalizadosExtensionType)
MSDN XmlSerializer.Deserialize()
Same error persists.