0

I'm writing an ASP.NET Core 7 app and my project now requires me to process an XML file version 1.1.

After searching for a while, I've seen that there is no support from the standard Microsoft libraries, as they only seem to support XML version 1.0.

The alternative, which is to use a third party library, they all seem to be paid solutions.

Is there any free solution (which preferably doesn't require any third party libraries) for being able read an XML version 1.1 in C# (or other)? How can I achieve this?


My sample XML

<?xml version="1.1" encoding="UTF-8"?>
<sample>
    <element>Contents of element 1</element>
    <element>Contents of element 2</element>
    <element>Contents of element 3</element>
</sample>

A fragment of my code to load the XML

XmlReaderSettings settings = new XmlReaderSettings();
// Create an instance of XmlReader to read the XML
XmlReader reader = XmlReader.Create(@"PATHTOMYXML", settings);

// Read the XML line by line
while (reader.Read()) {//This is the line that fails saying that it doesn't recognize the version 1.1
    // process the XML
}

// close XmlReader object
reader.Close();

Please, note that changing the version in the XML from 1.1 to 1.0 is not a suitable solution since it contains things from version 1.1.

The two features that appear in my XSD that are from version 1.1 are "assert" and "xSign" These two elements then allow me to check for a condition in the XML (assert) or being able to send a signature in the XML (xSign). Also I need the additional unicode support.

Thanks in advance

  • 1
    While asking a question, you need to provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example): Please edit your original question and provide the following: (1) Well-formed XML file sample. (2) What you need to do, i.e. logic, and your code attempt trying to implement it. (3) Desired output based on the sample data in #1 above. – Yitzhak Khabinsky Apr 27 '23 at 19:58
  • 1
    You can try modifying the xml contents as string changing version to 1.1 (it will work if no XML 1.1 features are used). – Guru Stron Apr 27 '23 at 20:08
  • @YitzhakKhabinsky Thanks for the feedback, I've just updated my post. – Alex Ibrahim Ojea Apr 27 '23 at 20:18
  • @GuruStron I'm afraid this is not a suitable solution since there are features from version 1.1 used – Alex Ibrahim Ojea Apr 27 '23 at 20:19
  • 1
    You can pre-process XML 1.1 via XSLT. After that it will digestible via c# – Yitzhak Khabinsky Apr 27 '23 at 23:40
  • 1
    Side note: @YitzhakKhabinsky this is how-to/library-request and not a debugging question - so code is not required. The reason we ask for [MRE] (or any other type of code) is to make sure OP done their research... Which in this particular case absolutely does not need code to show the research (and indeed the research for the known "no" answer was shown in the original version)... Wheather this should be treated as "how-to" or "get library" is open... but with "Is there any free solution" closing as "off-site resource request" looks fair. – Alexei Levenkov Apr 27 '23 at 23:52
  • @AlexeiLevenkov in my case what I'm actually trying to avoid is to use a library. As far as I've investigated, there doesn't seem to be any solution that doesn't involve the use of a paid library. My hopes of this post was to see how other developers have solved this issue / what is the best approach to achieve my goal... because I'm sure I'm not the first one that has tryed to process a XML version 1.1 in C# and I'm sure there must be at least one free solution. – Alex Ibrahim Ojea Apr 28 '23 at 20:06
  • 2
    @AlexIbrahimOjea What XML 1.1 features, specifically, do these documents use? – JLRishe Apr 28 '23 at 20:14
  • @JLRishe they use 2 features which are "assert" and "xSign" (I will add it to the question) – Alex Ibrahim Ojea Apr 28 '23 at 20:17
  • @AlexIbrahimOjea since you can't use someone else's code and know that there is no code you are looking for in .Net/.NetCore there is only one option left - to write it yourself... Looking for help with writing whole XML parser is too broad for SO - you probably should just post "no available in .Net up to 7" as an answer and go ahead to write your own (XML spec is pretty detailed and you can test your implementation against existing onces - easier than the very first one... Pay attention to normalization, including whitespace one as you seem to be interested in signatures). – Alexei Levenkov Apr 28 '23 at 21:44
  • 3
    @AlexIbrahimOjea I'm not really familiar with XML 1.1, but the XML 1.1 spec doesn't make any mention of "assert" or "xSign", so it's unclear to me what you're referring to. Can you share an example of an XML you are using that has these features? The XML sample you provided is pretty much XML 1.0, aside from having "1.1" instead of "1.0" in the first line. And as far as I can tell, [the only substantial difference](https://www.w3.org/TR/2006/REC-xml11-20060816/#sec-xml11) between 1.0 and 1.1 is that 1.1 has additional unicode support, nothing to do with assert or xSign. – JLRishe Apr 29 '23 at 02:54
  • @JLRishe sorry, actually the one who contains those elements is the XSD, which is then used for special cases in the XML. In the XML you would be able to send a signature (xSign) or when processing it, check for conditionals (assert) – Alex Ibrahim Ojea Apr 29 '23 at 06:30
  • Since you mention "assert", it appears that this is about XSD 1.1 rather than XML 1.1. See https://en.wikipedia.org/wiki/XML_Schema_(W3C)#Version_1.1 – mzjn Apr 29 '23 at 07:10
  • 1
    Psst, look here: https://stackoverflow.com/a/75444653/5045688. There are ways to use Saxon-HE for free. – Alexander Petrov Apr 29 '23 at 12:14
  • @AlexanderPetrov interesting... funny part is that there doesn't seem to be a lot of documentation on how to use it, and officially only supports up to .Net6. You can get the 30 free day trial – Alex Ibrahim Ojea Apr 29 '23 at 16:14

1 Answers1

1

As much I don like the answer I'm giving, because is not what was asked. Fact is, there's no solution in the constraints presented. Don't know what what any paid/free 3rd party promises but please stay away from dotnet xml version 1.1. 1.1 is not supported in dotnet (any framework).

There's still a solution that I used in couple of similar situations, and maybe it can be used here, it is dotnet who has to read xml version 1.1 no matter what, it is free (not really, nothing is free in this world but is not a tool you have to pay for something that I doubt will work) plus couple of peanuts for the hosting, so, here's my take.

Write a java utility app that does the reading/validation part (XML 1.1 was added in Java 6), maybe not Java DOM Parser but Java SAX Parser I think does it, not an expert when it comes to java, then integrate it in your solution as a container accessible over http as an API maybe a sidecar/component over http or grpc, still gRpc is not a good streaming transfer protocol if you deal with large xml's.

Finally, I had to give it a separate answer instead of an comment because it can be a solution for your problem.

SilentTremor
  • 4,747
  • 2
  • 21
  • 34
  • To me, XML 1.1 seems like a red herring. The question is actually about **XSD 1.1**, but the OP has not updated the question to clearly reflect that. – mzjn May 02 '23 at 12:58
  • @mzjn missed that part, If I remember correct from Android think Xerxes does for free the XSD verification, and the so called solution can work. – SilentTremor May 03 '23 at 06:45
  • 1
    Related: https://stackoverflow.com/a/61294027/407651 – mzjn May 03 '23 at 07:57
  • I'm afraid this does not cover the solution that I'm looking for. I don't want to have in an additional program written in Java for being just able to read an XML or interpret XSD of version 1.1. There must be a "cleaner" solution to this issue... – Alex Ibrahim Ojea May 03 '23 at 20:34