UblSharp / UblSharp

C# / .NET / XML library for working with OASIS UBL 2.0/2.1 documents
https://github.com/UblSharp/UblSharp
Other
95 stars 54 forks source link

InvalidOperationException: <UtilityStatement xmlns='urn:oioubl:names:specification:oioubl:schema:xsd:UtilityStatement-2'> was not expected. #31

Open lupusbytes opened 3 years ago

lupusbytes commented 3 years ago

I'm trying to load a UtilityStatement document using var result = UblDocument.Load<UtilityStatementType>(new FileStream(...), FileMode.Open, FileAccess.Read)

The document starts like this.

<?xml version="1.0" encoding="UTF-8"?>
<UtilityStatement xmlns="urn:oioubl:names:specification:oioubl:schema:xsd:UtilityStatement-2" xmlns:cac="urn:oioubl:names:specification:oioubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oioubl:names:specification:oioubl:schema:xsd:CommonBasicComponents-2" xmlns:ccts="urn:un:unece:uncefact:documentation:2" xmlns:sdt="urn:oioubl:names:specification:oioubl:schema:xsd:SpecializedDatatypes-2" xmlns:udt="urn:un:unece:uncefact:data:specification:UnqualifiedDataTypesSchemaModule:2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:oioubl:names:specification:oioubl:schema:xsd:UtilityStatement-2 UBL-UtilityStatement-2.1.xsd">
  <cbc:UBLVersionID>2.0</cbc:UBLVersionID>
  <cbc:CustomizationID>OIOUBL-2.02</cbc:CustomizationID>
  <cbc:ProfileID schemeAgencyID="320" schemeID="urn:oioubl:id:profileid-1.3">Reference-Utility-1.0</cbc:ProfileID>
  <cbc:ID></cbc:ID>
  <cbc:IssueDate>2021-01-01</cbc:IssueDate>
  <cbc:UtilityStatementTypeCode listAgencyID="320" listID="urn:oioubl:codelist:utilitystatementtypecode-1.0">Fibernet</cbc:UtilityStatementTypeCode>
  <cbc:DocumentCurrencyCode>DKK</cbc:DocumentCurrencyCode>
....

I get an InvalidOperationException

System.InvalidOperationException
  HResult=0x80131509
  Message=There is an error in XML document (2, 2).
  Source=System.Xml
  StackTrace:
   at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
   at UblSharp.UblDocument.Load[T](XmlReader reader)
   at UblSharp.UblDocument.Load[T](Stream stream)
   at ConsoleApp1.Program.Main(String[] args) in C:\Users\user\Documents\Projects\ConsoleApp1\Program.cs:line 16

Inner Exception 1:
InvalidOperationException: <UtilityStatement xmlns='urn:oioubl:names:specification:oioubl:schema:xsd:UtilityStatement-2'> was not expected.

Online validators like http://www.oioubl.net/validator/ confirms that the document is valid.

What is the reason for the exception ?

B-Art commented 1 year ago

In Pwsh I do: [xml]$a = Get-Content .\[filename].xml So your var should be some xml variable upfront. In .Net, C# something like:

internal static void ReadXMLFileUsingXMLDocument()
{
    XmlDocument xmlDcoument = new XmlDocument();

    xmlDcoument.Load(@"EmployeeData.xml");

    XmlNodeList? xmlNodeList = xmlDcoument.DocumentElement.SelectNodes("/Employees/Employee");

    Console.WriteLine("Output using XMLDocument");
    foreach (XmlNode xmlNode in xmlNodeList)
    {
        Console.WriteLine("Id of the Employee is : " + xmlNode.SelectSingleNode("Id").InnerText);
        Console.WriteLine("Name of the Employee is : " + xmlNode.SelectSingleNode("Name").InnerText);
        Console.WriteLine();
    }
}