0

For a code in C#, I am parsing a string to XML using XPathDocument.

The string is retrieved from SDL Trados Studio and it depends on the XML that is being worked on (how it was originally created and loaded for translations) the string sometimes has a BOM sometimes not.

Edit: The 'xml' is actually parsed from the segments of the source and target text and the structure element. The textual elements are escaped for xml and the markup and text is joined in one string. So if the markup has BOM in the xliff, then the string will have BOM.

I am trying to actually parse any of the xmls, independent of encoding. So at this point my solution is to remove the BOM with Substring.

Here is my code:

//Recreate XML files (extractor returns two string arrays)
string strSourceXML = String.Join("", extractor.TextSrc);
string strTargetXML = String.Join("", extractor.TextTgt);

//strip BOM
strSourceXML = strSourceXML.Substring(strSourceXML.IndexOf("<?"));
strTargetXML = strTargetXML.Substring(strSourceXML.IndexOf("<?"));

//Transform XML with the preview XSL
var xSourceDoc = new XPathDocument(strSourceXML);
var xTargetDoc = new XPathDocument(strTargetXML);

I have searched for a better solution, through several articles, such as these, but I found no better solution yet:

Any advice to solve this more elegantly?

Community
  • 1
  • 1
ib11
  • 2,530
  • 3
  • 22
  • 55
  • Maybe thia helps: http://stackoverflow.com/questions/3104158/xmlreader-breaks-on-utf-8-bom – Mattias Åslund May 14 '16 at 06:19
  • Open file with NotePad. Start NotePad and then browse for file using menu File : Open. When you click on filename check the encoding on the file in the NotePad Browser. If the encoding is not UTF8, open file and then save using UTF8. – jdweng May 14 '16 at 07:44
  • @jdweng You mean I should automate my string via Notepad?? Does not seem straightforward... – ib11 May 14 '16 at 07:52
  • You shouldn't ever have a 'BOM' in a string, the BOM should be recognised by the relevant decoder. The fact it exists is a sign that the file was decoded using the wrong encoding. Can you give more detail on how are you getting these strings? – Charles Mager May 14 '16 at 07:58
  • The square box is a unicode character which should not be in the UTF8 xml file. Not sure how the bad character got in the file. NotePad will remove character. You may need to find out why the bad character is occurring. – jdweng May 14 '16 at 07:59
  • @CharlesMager I updated the edit with the data where the string is from. – ib11 May 14 '16 at 08:29
  • 2
    If it's broken in your source, then there probably aren't many more elegant ways to fix it. I'd be tempted to prefer using [`TrimStart`](https://msdn.microsoft.com/en-us/library/system.string.trimstart(v=vs.110).aspx) to remove only the exact chars rather than hoping you find ``. – Charles Mager May 14 '16 at 10:12
  • @CharlesMager I am not really at control on what type of BOM the file has, but it for sure has an xml declaration on the first line. If I want to get into a trying to make it foolproof then with the circumstances, I think it is shorter to find the xml declaration than trying to enumerate [all the different BOM characters](https://en.wikipedia.org/wiki/Byte_order_mark#Representations_of_byte_order_marks_by_encoding), but thanks for the idea. – ib11 May 14 '16 at 18:38
  • 1
    Agree with @CharlesMager to try trimming the BOM. In my daily work as a localization engineer I only see three BOMs: `ef bb bf`, `ff fe`, and `fe ff`. Unless you have evidence that you need other variants I would stick with these three. – Jenszcz May 17 '16 at 07:07
  • @Jenszcz - Could you make your comment into an answer with a simple snippet with the exact `TrimStart` I should use? – ib11 May 19 '16 at 00:19
  • Removing various BOMs has already been discussed [here](http://stackoverflow.com/questions/1317700/strip-byte-order-mark-from-string-in-c-sharp), so I can't really take credit for that answer. :-) This might not work for you in case the BOM has undergone some weird conversion from byte array to string, but give it a try. – Jenszcz May 19 '16 at 08:12

1 Answers1

1

The constructor of XPathDocument taking a String argument https://msdn.microsoft.com/en-us/library/te0h7f95%28v=vs.110%29.aspx takes a URI with the XML file location. If you have a string with XML markup then use a StringReader over that string e.g.

XPathDocument xSourceDoc;
using (TextReader tr = new StringReader(strSourceXML))
{
  xSourceDoc = new XPathDocument(tr);
}
Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
  • 1
    Yeah, but that is the problem that my string has a BOM. So I am looking for means to strip it. Actually an existing method that recognizes BOMs, so I don't have to go the savage way of using `Substring`. – ib11 May 14 '16 at 18:40