public void Load(string file) { using (FileStream fs = File.OpenRead(file)) { byte[] buf = new byte [1024]; int nread, i; if ((nread = fs.Read(buf, 0, buf.Length)) <= 0) { return; } if (ByteOrderMark.TryParse(buf, nread, out bom)) { i = bom.Length; } else { i = 0; } do { // Read to the first newline to figure out which line endings this file is using while (i < nread) { if (buf[i] == '\r') { newLine = "\r\n"; break; } else if (buf[i] == '\n') { newLine = "\n"; break; } i++; } if (newLine == null) { if ((nread = fs.Read(buf, 0, buf.Length)) <= 0) { newLine = "\n"; break; } i = 0; } } while (newLine == null); // Check for a blank line at the end endsWithEmptyLine = fs.Seek(-1, SeekOrigin.End) > 0 && fs.ReadByte() == (int)'\n'; } // Load the XML document doc = new XmlDocument(); doc.PreserveWhitespace = false; // HACK: XmlStreamReader will fail if the file is encoded in UTF-8 but has <?xml version="1.0" encoding="utf-16"?> // To work around this, we load the XML content into a string and use XmlDocument.LoadXml() instead. string xml = File.ReadAllText(file); doc.LoadXml(xml); }
public static TextFormatInfo GetTextFormatInfo(string file) { var info = new TextFormatInfo(); string newLine = null; ByteOrderMark bom; using (FileStream fs = File.OpenRead(file)) { byte[] buf = new byte [1024]; int nread, i; if ((nread = fs.Read(buf, 0, buf.Length)) <= 0) { return(info); } if (ByteOrderMark.TryParse(buf, nread, out bom)) { i = bom.Length; } else { i = 0; } do { // Read to the first newline to figure out which line endings this file is using while (i < nread) { if (buf[i] == '\r') { newLine = "\r\n"; break; } else if (buf[i] == '\n') { newLine = "\n"; break; } i++; } if (newLine == null) { if ((nread = fs.Read(buf, 0, buf.Length)) <= 0) { newLine = "\n"; break; } i = 0; } } while (newLine == null); // Check for a blank line at the end info.EndsWithEmptyLine = fs.Seek(-1, SeekOrigin.End) > 0 && fs.ReadByte() == (int)'\n'; info.NewLine = newLine; info.ByteOrderMark = bom; return(info); } }
public void Read(Stream stream, string encoding) { ByteOrderMark bom = null; byte[] content = null; long nread; retry: stream.Seek(0, SeekOrigin.Begin); if (encoding == null) { if (ByteOrderMark.TryParse(stream, out bom)) { stream.Seek(bom.Length, SeekOrigin.Begin); } else { stream.Seek(0, SeekOrigin.Begin); } } content = new byte [bom != null ? stream.Length - bom.Length : stream.Length]; nread = 0; int n; while ((n = stream.Read(content, (int)nread, (content.Length - (int)nread))) > 0) { nread += n; } if (encoding != null) { string s = ConvertFromEncoding(content, nread, encoding); if (s == null) { // The encoding provided was wrong, fall back to trying to use the BOM if it exists... encoding = null; content = null; goto retry; } text = new StringBuilder(s); sourceEncoding = encoding; return; } if (bom != null) { string s = ConvertFromEncoding(content, nread, bom.Name); if (s != null) { HadBOM = true; sourceEncoding = bom.Name; text = new StringBuilder(s); return; } } // Fall back to trying all the encodings... foreach (TextEncoding co in TextEncoding.ConversionEncodings) { string s = ConvertFromEncoding(content, nread, co.Id); if (s != null) { sourceEncoding = co.Id; text = new StringBuilder(s); return; } } throw new Exception("Unknown text file encoding"); }
public void Read(FilePath fileName, string encoding) { // Reads the file using the specified encoding. // If the encoding is null, it autodetects the // required encoding. this.name = fileName; ByteOrderMark bom = null; byte[] content = null; long nread; retry: using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)) { if (encoding == null) { if (ByteOrderMark.TryParse(stream, out bom)) { stream.Seek(bom.Length, SeekOrigin.Begin); } else { stream.Seek(0, SeekOrigin.Begin); } } content = new byte [bom != null ? stream.Length - bom.Length : stream.Length]; nread = 0; int n; while ((n = stream.Read(content, (int)nread, (content.Length - (int)nread))) > 0) { nread += n; } } if (encoding != null) { string s = ConvertFromEncoding(content, nread, encoding); if (s == null) { // The encoding provided was wrong, fall back to trying to use the BOM if it exists... encoding = null; content = null; goto retry; } text = new StringBuilder(s); sourceEncoding = encoding; return; } else if (bom != null) { string s = ConvertFromEncoding(content, nread, bom.Name); if (s != null) { HadBOM = true; sourceEncoding = bom.Name; text = new StringBuilder(s); return; } } // Fall back to trying all the encodings... foreach (TextEncoding co in TextEncoding.ConversionEncodings) { string s = ConvertFromEncoding(content, nread, co.Id); if (s != null) { sourceEncoding = co.Id; text = new StringBuilder(s); return; } } throw new Exception("Unknown text file encoding"); }