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?

12
  • Maybe thia helps: stackoverflow.com/questions/3104158/… Commented May 14, 2016 at 6: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. Commented May 14, 2016 at 7:44
  • @jdweng You mean I should automate my string via Notepad?? Does not seem straightforward... Commented May 14, 2016 at 7:52
  • 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 to remove only the exact chars rather than hoping you find <?. Commented May 14, 2016 at 10:12
  • 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. Commented May 17, 2016 at 7:07

1 Answer 1

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); } 
Sign up to request clarification or add additional context in comments.

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.