public void WritePictureXMLFile() { GlobalDataStore.Logger.Debug("Updating Pictures.xml ..."); List <LabelX.Toolbox.LabelXItem> items = new List <LabelX.Toolbox.LabelXItem>(); DirectoryInfo PicturesRootFolderDirectoryInfo = new DirectoryInfo(PicturesRootFolder); LabelX.Toolbox.Toolbox.GetPicturesFromFolderTree(PicturesRootFolderDirectoryInfo.FullName, ref items); //items = alle ingelezen pictures. Nu gaan wegschrijven. System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); System.Xml.XmlElement root = doc.CreateElement("LabelXItems"); foreach (LabelX.Toolbox.LabelXItem item in items) { System.Xml.XmlElement itemXML = doc.CreateElement("item"); itemXML.SetAttribute("name", item.Name); itemXML.SetAttribute("hash", item.Hash); root.AppendChild(itemXML); } doc.AppendChild(root); MemoryStream ms = new MemoryStream(); System.Xml.XmlTextWriter tw = new System.Xml.XmlTextWriter(ms, Encoding.UTF8); tw.Formatting = System.Xml.Formatting.Indented; doc.WriteContentTo(tw); doc.Save(PicturesRootFolder + "pictures.xml"); tw.Close(); }
public static string FormatXmlDocument(string xml) { var xmlDocument = new System.Xml.XmlDocument(); xmlDocument.LoadXml(xml); var stream = new System.IO.MemoryStream(); var writer = new System.Xml.XmlTextWriter(stream, Encoding.Unicode); // indent setting writer.Formatting = System.Xml.Formatting.Indented; writer.Indentation = 2; // indent length writer.IndentChar = ' '; // indent character // formatting write xmlDocument.WriteContentTo(writer); writer.Flush(); stream.Flush(); stream.Position = 0; // to string var reader = new System.IO.StreamReader(stream); string formattedXml = reader.ReadToEnd(); return(formattedXml); }
private String GetPrintXML(String xml) { if (String.IsNullOrEmpty(xml)) { return(String.Empty); } /*try * { * System.Xml.Linq.XDocument doc = System.Xml.Linq.XDocument.Parse(xml); * return doc.ToString(); * } * catch (Exception) * { * return xml; * }*/ String Result = String.Empty; using (System.IO.MemoryStream mStream = new System.IO.MemoryStream()) { System.Xml.XmlTextWriter writer = new System.Xml.XmlTextWriter(mStream, Encoding.UTF8); System.Xml.XmlDocument document = new System.Xml.XmlDocument(); try { // Load the XmlDocument with the XML. document.LoadXml(xml); writer.Formatting = System.Xml.Formatting.Indented; // Write the XML into a formatting XmlTextWriter document.WriteContentTo(writer); writer.Flush(); mStream.Flush(); // Have to rewind the MemoryStream in order to read // its contents. mStream.Position = 0; // Read MemoryStream contents into a StreamReader. System.IO.StreamReader sReader = new System.IO.StreamReader(mStream); // Extract the text from the StreamReader. String FormattedXML = sReader.ReadToEnd(); Result = FormattedXML; } catch (System.Xml.XmlException) { return(xml); } finally { writer.Close(); } } return(Result); }
/// <summary> /// 保存对象数据到XML书写器中 /// </summary> /// <param name="myWriter">XML书写器</param> /// <returns>操作是否成功</returns> protected override bool InnerWrite(System.Xml.XmlWriter myWriter) { if (myXMLDocument.DocumentElement == null) { myWriter.WriteString(this.strLoadErrorMsg); } else { myXMLDocument.WriteContentTo(myWriter); } return(true); }
internal void FormatXml() { System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); doc.LoadXml(Editor.Text); System.IO.StringWriter sw = new System.IO.StringWriter(); using (System.Xml.XmlTextWriter writer = new System.Xml.XmlTextWriter(sw)) { writer.Indentation = 2; // the Indentation writer.Formatting = System.Xml.Formatting.Indented; doc.WriteContentTo(writer); writer.Close(); } Editor.Text = sw.ToString(); }
public static string XmlBeautifier(string xml) { string result = ""; try { System.Xml.XmlDocument document = new System.Xml.XmlDocument(); document.XmlResolver = null; // Load the XmlDocument with the XML. document.LoadXml(xml); using (System.IO.MemoryStream mStream = new System.IO.MemoryStream()) { using (System.Xml.XmlTextWriter writer = new System.Xml.XmlTextWriter(mStream, System.Text.Encoding.Unicode)) { writer.Formatting = System.Xml.Formatting.Indented; // Write the XML into a formatting XmlTextWriter document.WriteContentTo(writer); writer.Flush(); mStream.Flush(); // Have to rewind the MemoryStream in order to read // its contents. mStream.Position = 0; // Read MemoryStream contents into a StreamReader. using (System.IO.StreamReader sReader = new System.IO.StreamReader(mStream)) { // Extract the text from the StreamReader. result = sReader.ReadToEnd(); } // End Using sReader } // End Using writer } // End Using mStream } catch (System.Xml.XmlException) { // Handle the exception } return(result); } // End Function XmlBeautifier
public void WriteDirectoryXMLFile(string folder, string fnaam) { System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); sw.Start(); GlobalDataStore.Logger.Debug("Updating " + fnaam + " ..."); List <LabelX.Toolbox.LabelXItem> items = new List <LabelX.Toolbox.LabelXItem>(); DirectoryInfo FolderDirectoryInfo = new DirectoryInfo(folder); LabelX.Toolbox.Toolbox.GetFilesFromFolderTree(FolderDirectoryInfo.FullName, ref items); //items = alle ingelezen bestanden. Nu gaan wegschrijven. System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); System.Xml.XmlElement root = doc.CreateElement("LabelXItems"); foreach (LabelX.Toolbox.LabelXItem item in items) { //if( getFileExt(item.Name) == ".xml") continue; if (!item.Name.Equals(folder + fnaam)) { System.Xml.XmlElement itemXML = doc.CreateElement("item"); itemXML.SetAttribute("name", item.Name); itemXML.SetAttribute("hash", item.Hash); root.AppendChild(itemXML); } } doc.AppendChild(root); MemoryStream ms = new MemoryStream(); System.Xml.XmlTextWriter tw = new System.Xml.XmlTextWriter(ms, Encoding.UTF8) { Formatting = System.Xml.Formatting.Indented }; doc.WriteContentTo(tw); doc.Save(folder + fnaam); tw.Close(); sw.Stop(); GlobalDataStore.Logger.Debug("Creating the XML Hash file for the directory took: " + sw.ElapsedMilliseconds + " ms (" + folder + ")"); sw.Reset(); }
/// <summary> /// Formats the provided XML so it's indented and humanly-readable. /// </summary> /// <param name="inputXml">The input XML to format.</param> /// <returns></returns> public static string FormatXml(string xml, bool omitXmlDeclaration, string indentChars) { // XmlDocument.LoadXml sadece tam ve net sentaxlý xml ler için // string den oluþan ve xml gibi görünen tüm genel xml ler için aþaðýdaki gibi kullanmak gerekiyormuþ // çünkü stringler UTF-16 ile çalýþýrlarmýþ, sorun çýkmamasý için aþaðýdaki gibi yapýyoruz. // http://stackoverflow.com/questions/310669/why-does-c-xmldocument-loadxmlstring-fail-when-an-xml-header-is-included // Aþaðýdaki implementasyon bu soruna çözüm oluyor byte[] encodedString = System.Text.Encoding.UTF8.GetBytes(xml); System.Xml.XmlDocument document = new System.Xml.XmlDocument(); // Put the byte array into a stream and rewind it to the beginning using (MemoryStream ms = new MemoryStream(encodedString)) { ms.Flush(); ms.Position = 0; document.Load(ms); } System.Xml.XmlWriterSettings settings = new System.Xml.XmlWriterSettings(); settings.Indent = true; if (indentChars != null) { settings.IndentChars = indentChars; // settings.IndentChars = "\t"; } settings.OmitXmlDeclaration = omitXmlDeclaration; // üst xml deklarasyonu kaldýrýr //settings.NewLineOnAttributes = false; //settings.Encoding = encoding; System.Text.StringBuilder output = new System.Text.StringBuilder(); using (System.Xml.XmlWriter writer = System.Xml.XmlWriter.Create(output, settings)) { document.WriteContentTo(writer); writer.Flush(); } return(output.ToString()); }
List <Result> handleXML(string text) { try { System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); doc.LoadXml(text); string fmt = null, zip = null; { StringWriter sw = new System.IO.StringWriter(); using (System.Xml.XmlTextWriter writer = new System.Xml.XmlTextWriter(sw)) { writer.Indentation = 4; // the Indentation writer.Formatting = System.Xml.Formatting.Indented; doc.WriteContentTo(writer); writer.Close(); } fmt = sw.ToString(); } { StringWriter sw = new StringWriter(); using (System.Xml.XmlTextWriter writer = new System.Xml.XmlTextWriter(sw)) { doc.WriteContentTo(writer); writer.Close(); } zip = sw.ToString(); } Result fmtItem = new Result() { Title = "复制格式化后的XML文本", SubTitle = abbr(fmt), IcoPath = "Images\\format.png", Action = e => { Clipboard.SetDataObject(fmt); return(true); } }; Result zipItem = new Result() { Title = "复制压缩后的XML文本", SubTitle = abbr(zip), IcoPath = "Images\\zip.png", Action = e => { Clipboard.SetDataObject(zip); return(true); } }; Result viewItem = new Result() { Title = "在浏览器中查看XML结构", IcoPath = "Images\\browser.png", Action = e => { File.WriteAllText(Path.Combine(pluginDir, XMLJsFile), "var xmlData=`" + tobs64(fmt) + "`"); Process.Start(Path.Combine(pluginDir, XMLHtmlFile)); return(true); } }; return(new List <Result> { fmtItem, zipItem, viewItem }); } catch { return(null); } }
private string CreateRowXml(string vergelijking, string[] fieldnames, string[] fieldvalues) { var doc = new System.Xml.XmlDocument(); /* * if (Properties.Settings.Default.output_format == "html") * { */ var table = doc.CreateElement("table"); var tablename = doc.CreateAttribute("name"); tablename.Value = vergelijking.ToLower(); table.Attributes.Append(tablename); doc.AppendChild(table); for (int i = 0; i < fieldnames.Length; i++) { var tr = doc.CreateElement("tr"); table.AppendChild(tr); var tdname = doc.CreateElement("td"); tdname.InnerText = fieldnames[i].ToLower(); tr.AppendChild(tdname); var tdvalue = doc.CreateElement("td"); if (fieldvalues[i] == null || Convert.ToString(fieldvalues[i]).Length == 0) { // we moeten er iets in hebben staan om de uitlijning altijd goed te houden tdvalue.InnerXml = " "; } else { tdvalue.InnerText = fieldvalues[i]; } tr.AppendChild(tdvalue); } /* * } * else { * var rootnode = doc.CreateElement(System.Xml.XmlConvert.EncodeName(vergelijking).ToLower()); * if (fieldnames.Length > 0) * { * for(int i=0; i < fieldnames.Length; i++) * { * var element = doc.CreateElement(System.Xml.XmlConvert.EncodeName(fieldnames[i]).ToLower()); * if(fieldvalues[i] == null || Convert.ToString(fieldvalues[i]).Length == 0) * { * var nil = doc.CreateAttribute("nil", "xsi", "http://w3.org/2001/XMLSchema-instance"); * nil.Value = Convert.ToString(true); * element.Attributes.Append(nil); * } * else * { * element.InnerText = fieldvalues[i]; * } * rootnode.AppendChild(element); * } * } * else * { * var nil = doc.CreateAttribute("nil", "xsi", "http://w3.org/2001/XMLSchema-instance"); * nil.Value = Convert.ToString(true); * rootnode.Attributes.Append(nil); * } * doc.AppendChild(rootnode); * } */ var stream = new System.IO.MemoryStream(); var writer = new System.Xml.XmlTextWriter(stream, Encoding.Unicode); writer.Formatting = System.Xml.Formatting.Indented; doc.WriteContentTo(writer); writer.Flush(); stream.Flush(); stream.Position = 0; var reader = new System.IO.StreamReader(stream); return(reader.ReadToEnd()); }