public void PrepareTest() { var parentDirectory = Directory.GetParent(new Uri(GetType().Assembly.Location).LocalPath).FullName; var fileName = Path.Combine(parentDirectory, "Resources", "test_with_umlaut_and_wrong_encoding.xml"); _objectUnderTest = Parser.Parse(fileName); _root = _objectUnderTest.Children.Single(); }
public void PrepareTest() { var parentDirectory = Directory.GetParent(new Uri(GetType().Assembly.Location).LocalPath).FullName; var fileName = Path.Combine(parentDirectory, "Resources", "CommentWithoutDeclaration.xml"); _objectUnderTest = Parser.Parse(fileName); _root = _objectUnderTest.Children.Single(); }
public static File ParseCore(string filePath, CharacterPositionFinder finder, IXmlFlavor flavor, Encoding encoding) { using (var reader = new XmlTextReader(new StreamReader(SystemFile.OpenRead(filePath), encoding))) { var file = new File { Name = filePath, FooterSpan = CharacterSpan.None, // there is no footer }; var fileBegin = new LineInfo(reader.LineNumber + 1, reader.LinePosition); try { var dummyRoot = new Container(); // Parse the XML and display the text content of each of the elements. // as there are XMLs that have the declaration on same line as the root element, we just loop and parse until the end while (!reader.EOF) { Parse(reader, dummyRoot, finder, flavor); } var root = dummyRoot.Children.OfType <Container>().Last(); var rootEnd = FixRoot(root, dummyRoot); var fileEnd = new LineInfo(reader.LineNumber, reader.LinePosition - 1); var positionAfterLastElement = new LineInfo(rootEnd.LineNumber, rootEnd.LinePosition + 1); // we calculate the next one (either a new line character or a regular one) file.LocationSpan = new LocationSpan(fileBegin, fileEnd); if (positionAfterLastElement < fileEnd) { file.FooterSpan = GetCharacterSpan(new LocationSpan(positionAfterLastElement, fileEnd), finder); } file.Children.Add(root); } catch (XmlException ex) { // try to adjust location span to include full file content // but ignore empty files as parsing errors var lines = SystemFile.ReadLines(filePath).Count(); if (lines == 0) { file.LocationSpan = new LocationSpan(LineInfo.None, LineInfo.None); } else { file.ParsingErrors.Add(new ParsingError { ErrorMessage = ex.Message, Location = new LineInfo(ex.LineNumber, ex.LinePosition), }); file.LocationSpan = new LocationSpan(new LineInfo(1, 0), new LineInfo(lines + 1, 0)); } } return(file); } }