0

I need to encode normal XML file to Fast Infoset XML document. In Nuget package, I installed LiquidTechnologies.FastInfoset package (which is the only one I found)

My XML doc to convert as below

<?xml version="1.0" encoding="utf-8"?> <GomsStockRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" callerMethod="pushInfinityAllocatedStockLevel" transactionType="InventoryJob"> <stockRequestEntry> <productCode>100001</productCode>     <availableQuantity>1</availableQuantity>     <reservedQuantity>0</reservedQuantity>     <inventoryType>Job_Import</inventoryType>   </stockRequestEntry> </GomsStockRequest>

There are CR LF and white space characters after transactionType="InventoryJob"> and before the child elements. My destination system is using Java and they can read the Fast Infoset docs encoded by Java as following:

à  � xÏ�xsi(http://www.w3.org/2001/XMLSchema-instanceÏ�xsd�http://www.w3.org/2001/XMLSchemað<�GomsStockRequestx�callerMethodH�pushInfinityAllocatedStockLevelx�transactionTypeH�InventoryJobð’  <�stockRequestEntry’� < productCode’�100001ð’�     <�availableQuantity�1ð£<�reservedQuantity�0ð£<inventoryType’�Job_Importð’�   ð� ÿ

which preserved the whitespace and maybe just ignore the CR char.

However, for me, using C#, the result after write out Fast Infoset doc as following:

à  � xÏ�xsi(http://www.w3.org/2001/XMLSchema-instanceÏ�xsd�http://www.w3.org/2001/XMLSchemað<�GomsStockRequestx�callerMethodH�pushInfinityAllocatedStockLevelx�transactionTypeH�InventoryJobð<�stockRequestEntry< productCode’�100001ð<�availableQuantity�1ð<�reservedQuantity�0ð<inventoryType’�Job_Importÿ

which seems that the whitespace and CR character are trimmed

My models are below

public class GomsStockRequest
    {
        [XmlAttribute("callerMethod")]
        public string callerMethod { get; set; }

        [XmlAttribute("transactionType")]
        public string transactionType { get; set; }
        public StockRequestEntry stockRequestEntry { get; set; }
    }

    public class StockRequestEntry
    {
        public string productCode { get; set; }
        public int availableQuantity { get; set; }
        public int reservedQuantity { get; set; }
        public string inventoryType { get; set; }

    }

The program is as below:

var gomsstockrequest = new GomsStockRequest()
            {
                callerMethod = "pushInfinityAllocatedStockLevel",
                transactionType = "InventoryJob",
                stockRequestEntry = new StockRequestEntry()
                {
                    productCode = "100001",
                    availableQuantity = 1,
                    reservedQuantity = 0,
                    inventoryType = "Job_Import"
                    
                }
            };

var serializer = new XmlSerializer(typeof(GomsStockRequest));
XmlWriter fwriter = XmlWriter.Create(new FIWriter("fiGomsStockRequest.xml"));
            serializer.Serialize(fwriter, gomsstockrequest);

Does anyone have experience in this encoding?

When I view the data in Notepad++, the difference is as in screenshot

Difference of Fast Infoset document between Java and C#

Please advise. Thank you

Hai Nguyen
  • 25
  • 4
  • 1
    First, whitespace *between* elements in XML is insignificant. Why is it important to have it in the output in the first place? Second, the code you're showing here is serializing a class, not attempting to convert input XML. If you have input that has whitespace to preserve, you can't go through a deserialization step first, as you would lose it no matter what else you do. – Jeroen Mostert Feb 23 '23 at 11:02
  • Thank you @Jeroen Mostert, Actually, my Fast Infoset document when uploading to our system got error as below: "message" : "javax.xml.bind.UnmarshalException - with linked exception: [javax.xml.stream.XMLStreamException: java.io.EOFException: Unexpeceted EOF]", "msgCode" : "E111042", when I compare with the Fast Infoset generated by that System, it seems to be trimmed the whitespace – Hai Nguyen Feb 23 '23 at 12:57
  • @JeroenMostert I have changed to write the XML to file, then read byte from file to send, it's successfully. I'm not sure what is the difference between serialize to Fast Infocode directly from a class to encode in the stream and write to file then encode? But it works with file load method. Thank you – Hai Nguyen Feb 23 '23 at 15:10

1 Answers1

1

When using LiquidTechnologies.FastInfoset, insignificant whitespace is ignored when encoding XML data as FastInfoset, as it has no practical value.

The FI Encoder has no option to turn on the encoding of insignificant whitespace.

If you want a formatted XML document, then you can format the XML data once it has been FI decoded using the Microsoft .Net Framework Xml classes.

  • I have added a screenshot about 2 Fast Infoset document in my question: the left one is encoded by Java, the right one is encoded by C# with your library. The problem is that when I send mine, I got error of message" : "javax.xml.bind.UnmarshalException - with linked exception: [javax.xml.stream.XMLStreamException: java.io.EOFException: Unexpeceted EOF] That's why I think the whitespace and CR character cause the error – Hai Nguyen Feb 23 '23 at 13:06
  • I have tested that the whitespace is not the root cause, it's the way I serialize directly from class to FI then convert to stream. I must use writing to file method, then read from file successfully. Thank you – Hai Nguyen Feb 23 '23 at 15:12