Results 1 to 3 of 3

Thread: xml deserialization error in namespace or root directory

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2013
    Posts
    24

    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

  2. #2
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,024

    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:
    1. norm = CType(dcmd.Deserialize(reader), NormalizadosExtensionType)

    MSDN XmlSerializer.Deserialize()
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jan 2013
    Posts
    24

    Re: xml deserialization error in namespace or root directory

    Quote Originally Posted by KGComputers View Post
    Checking through MSDN docs, you need to restore the object's state when performing deserialization.

    VB.NET Code:
    1. norm = CType(dcmd.Deserialize(reader), NormalizadosExtensionType)

    MSDN XmlSerializer.Deserialize()


    Same error persists.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width