static void Main(string[] args) { // first you have to create the xml file to any location XmlTextWriter textWriter = new XmlTextWriter("D:\\TestxmlFile.xml", null); // to write any things you have to Opens the document textWriter.WriteStartDocument(); // Write first element textWriter.WriteStartElement("Person"); textWriter.WriteStartElement("r", "RECORD", "urn:record"); // Write next element textWriter.WriteStartElement("Email", ""); textWriter.WriteString("DOB"); textWriter.WriteString("City"); textWriter.WriteEndElement(); // WriteChars string[] ch = new string[3]; ch[0] = "*****@*****.**"; ch[1] = "YYYY-MM-DD"; ch[2] = "xyz"; textWriter.WriteStartElement("Char"); textWriter.WriteChars(ch, 0, ch.Length); textWriter.WriteEndElement(); // Ends the document. textWriter.WriteEndDocument(); // close writer textWriter.Close(); }
public void createNwriteXML() { XmlTextWriter textWriter = new XmlTextWriter("created.xml", null); //Opens the document textWriter.WriteStartDocument(); //Write comment textWriter.WriteComment("First comment XMLTextWriter sample Example"); textWriter.WriteComment("myXmlFile.xml in root dir"); //Write first element textWriter.WriteStartElement("Student"); textWriter.WriteStartElement("r", "Record", "urn:record"); //Write next element textWriter.WriteStartElement("Name", ""); textWriter.WriteString("Student"); textWriter.WriteEndElement(); //WriteChars char[] ch = new char[3]; ch[0] = 'a'; ch[1] = 'r'; ch[2] = 'c'; textWriter.WriteStartElement("Char"); textWriter.WriteChars(ch, 0, ch.Length); textWriter.WriteEndElement(); //End the document textWriter.WriteEndElement(); //Close Writer textWriter.Close(); }
static void WriteXml_XmlTextWritter() { // filename string filename = AppDomain.CurrentDomain.BaseDirectory + "Laptop.xml"; // Create a new file in C:\\ dir XmlTextWriter textWriter = new XmlTextWriter(filename, null); // Opens the document textWriter.WriteStartDocument(); // Write comments textWriter.WriteComment("First Comment XmlTextWriter Sample Example"); textWriter.WriteComment("myXmlFile.xml in root dir"); // Write first element textWriter.WriteStartElement("Student"); //textWriter.WriteStartElement("r", "RECORD", "urn:record"); // Write next element textWriter.WriteStartElement("Name", ""); textWriter.WriteString("Student"); textWriter.WriteEndElement(); // Write one more element textWriter.WriteStartElement("Address", ""); textWriter.WriteString("Colony"); textWriter.WriteEndElement(); // WriteChars char[] ch = new char[3]; ch[0] = 'a'; ch[1] = 'r'; ch[2] = 'c'; textWriter.WriteStartElement("Char"); textWriter.WriteChars(ch, 0, ch.Length); textWriter.WriteEndElement(); // write users textWriter.WriteStartElement("users"); textWriter.WriteStartElement("user"); textWriter.WriteAttributeString("age", "42"); textWriter.WriteString("John Doe"); textWriter.WriteEndElement(); textWriter.WriteStartElement("user"); textWriter.WriteAttributeString("age", "39"); textWriter.WriteString("Jane Doe"); // Ends the document. textWriter.WriteEndDocument(); // close writer textWriter.Close(); }
/// <summary> /// Converts the object passed in to its XML representation. /// The XML string is written on the XmlTextWriter. /// </summary> public void ToXml(object value, FieldInfo field, XmlTextWriter xml, IMarshalContext context) { Type type = value.GetType(); if (value is char[]) { char[] buffer = value as char[]; context.WriteStartTag(__arrayType, field, xml); xml.WriteChars(buffer, 0, buffer.Length); context.WriteEndTag(__arrayType, field, xml); } else { context.WriteStartTag(__type, field, xml); xml.WriteString(value.ToString()); context.WriteEndTag(__type, field, xml); } }
protected static string GetBase64StringForImage(string imgPath) { byte[] imageBytes = System.IO.File.ReadAllBytes(imgPath); string base64String = Convert.ToBase64String(imageBytes); string lines = base64String; System.IO.File.WriteAllText(@"E:\_MIS_Moalemi\ECE\GetBase64StringFromImage\GetBase64StringFromImage\WriteLines2.txt", lines); // Create a new file in C:\\ dir XmlTextWriter ECEWriter = new XmlTextWriter("E:\\myXmFile.xml", null); // Opens the document ECEWriter.WriteStartDocument(); // Write comments ECEWriter.WriteComment("First Comment XmlTextWriter ECE Example"); ECEWriter.WriteComment("myXmlFile.xml in E root dir"); // Write first element ECEWriter.WriteStartElement(".PNG ImageAttach"); ECEWriter.WriteStartElement("r", "RECORD", "record"); // Write next element ECEWriter.WriteStartElement("Name", ""); ECEWriter.WriteString(base64String); ECEWriter.WriteEndElement(); // Write one more element ECEWriter.WriteStartElement("Address", ""); ECEWriter.WriteString("*****@*****.**"); ECEWriter.WriteEndElement(); // WriteChars char[] ch = new char[4]; ch[0] = 'M'; ch[1] = 'A'; ch[2] = 'I'; ch[3] = 'L'; ECEWriter.WriteStartElement("Char"); ECEWriter.WriteChars(ch, 0, ch.Length); ECEWriter.WriteEndElement(); // Ends the document. ECEWriter.WriteEndDocument(); // close writer ECEWriter.Close(); return(lines); }
static void Main() { //<snippet1> using (XmlTextWriter writer = new XmlTextWriter(Console.Out)) { writer.WriteStartDocument(); char[] ch = new char[4]; ch[0] = 't'; ch[1] = 'e'; ch[2] = 'x'; ch[3] = 't'; writer.WriteStartElement("WriteCharacters"); writer.WriteChars(ch, 0, ch.Length); writer.WriteEndElement(); writer.WriteEndDocument(); } //</snippet1> }
/// <summary> /// Copies everything from the reader object to the writer instance /// </summary> /// <param name="writer">The XmlTextWriter to copy to.</param> /// <param name="reader">The XmlReader to read from.</param> /// <param name="defattr">true to copy the default attributes from the XmlReader; otherwise, false.</param> /// <param name="skipHtmlNode">是否跳过html节点</param> /// <param name="clearTag">是否清除html tag,只输出纯文本</param> /// <param name="maxCount">copy的文本的字符数,如果maxCount<=0,copy全部文本</param> /// <param name="endStr">如果只copy了部分文本,部分文本后的附加字符,如...</param> public static void WriteXml(XmlTextWriter writer, XmlReader reader, bool defattr, bool skipHtmlNode, bool clearTag, int maxCount, string endStr) { if (writer == null) { throw new ArgumentNullException("writer"); } if (reader == null) { throw new ArgumentNullException("reader"); } int count = 0; bool finished = false; reader.Read(); while (!finished && !reader.EOF) { char[] writeNodeBuffer = null; bool canReadValueChunk = reader.CanReadValueChunk; int num = (reader.NodeType == XmlNodeType.None) ? -1 : reader.Depth; do { if (clearTag && reader.NodeType != XmlNodeType.Text) { writer.WriteString(" "); continue; } switch (reader.NodeType) { case XmlNodeType.Element: if (skipHtmlNode && reader.Name != null && reader.Name.ToLower() == "html") { break; } writer.WriteStartElement(reader.Prefix, reader.LocalName, reader.NamespaceURI); writer.WriteAttributes(reader, defattr); if (reader.IsEmptyElement) { writer.WriteEndElement(); } break; case XmlNodeType.Text: int num2; if (!canReadValueChunk) { if (maxCount > 0) { string value = reader.Value; if ((count + value.Length) >= maxCount) { value = value.Substring(0, (maxCount - count)); if (!String.IsNullOrEmpty(endStr)) { value += endStr; } finished = true; } writer.WriteString(value); count += value.Length; } else { writer.WriteString(reader.Value); } break; } if (writeNodeBuffer == null) { writeNodeBuffer = new char[0x400]; } while ((num2 = reader.ReadValueChunk(writeNodeBuffer, 0, 0x400)) > 0) { if (maxCount > 0) { if ((count + num2) >= maxCount) { num2 = maxCount - count; writer.WriteChars(writeNodeBuffer, 0, num2); if (!String.IsNullOrEmpty(endStr)) { writer.WriteString(endStr); } finished = true; } else { writer.WriteChars(writeNodeBuffer, 0, num2); } count += num2; } else { writer.WriteChars(writeNodeBuffer, 0, num2); } } break; case XmlNodeType.CDATA: writer.WriteCData(reader.Value); break; case XmlNodeType.EntityReference: writer.WriteEntityRef(reader.Name); break; case XmlNodeType.ProcessingInstruction: case XmlNodeType.XmlDeclaration: writer.WriteProcessingInstruction(reader.Name, reader.Value); break; case XmlNodeType.Comment: writer.WriteComment(reader.Value); break; case XmlNodeType.DocumentType: writer.WriteDocType(reader.Name, reader.GetAttribute("PUBLIC"), reader.GetAttribute("SYSTEM"), reader.Value); break; case XmlNodeType.Whitespace: case XmlNodeType.SignificantWhitespace: writer.WriteWhitespace(reader.Value); break; case XmlNodeType.EndElement: if (skipHtmlNode && reader.Name != null && reader.Name.ToLower() == "html") { break; } writer.WriteFullEndElement(); break; } }while(!finished && reader.Read() && ((num < reader.Depth) || ((num == reader.Depth) && (reader.NodeType == XmlNodeType.EndElement)))); } }
public override void WriteChars(char[] buffer, int index, int count) { _originalWriter.WriteChars(buffer, index, count); _buffer.WriteChars(buffer, index, count); }
public override void WriteChars(char[] buffer, int index, int count) { xmlWriter.WriteChars(buffer, index, count); }
public override void WriteChars(Char[] buffer, Int32 index, Int32 count) { base.WriteChars(buffer, index, count); _logWriter.WriteChars(buffer, index, count); }
public override void WriteChars(char[] buffer, int index, int count) { _me.WriteChars(buffer, index, count); _bu.WriteChars(buffer, index, count); }
private static void GenerateXML() { // Generate Xml using XmlTextWriter XmlTextWriter xmlTextWriter = new XmlTextWriter(@"C:\Users\Mihu\Documents\XMLSample\XMLSample\student.xml", null); xmlTextWriter.Formatting = Formatting.Indented; xmlTextWriter.WriteStartDocument(); // add comments xmlTextWriter.WriteComment("My First comment in XML file"); xmlTextWriter.WriteStartElement("Student"); xmlTextWriter.WriteStartElement("r", "Record", "urn:record"); //write other element xmlTextWriter.WriteStartElement("Name", ""); xmlTextWriter.WriteString("Student 1"); xmlTextWriter.WriteEndElement(); xmlTextWriter.WriteStartElement("Address", ""); xmlTextWriter.WriteString("Camin 2"); xmlTextWriter.WriteEndElement(); char[] chArray = new char[3]; chArray[0] = 'a'; chArray[1] = 'b'; chArray[2] = 'c'; xmlTextWriter.WriteStartElement("Char"); xmlTextWriter.WriteChars(chArray, 0, chArray.Length); xmlTextWriter.WriteEndElement(); xmlTextWriter.WriteEndElement(); xmlTextWriter.Close(); //// Answer: xmlTextWriter = new XmlTextWriter(@"C:\Users\Mihu\Documents\XMLSample\XMLSample\answer.xml", null); xmlTextWriter.Formatting = Formatting.Indented; xmlTextWriter.WriteStartDocument(); xmlTextWriter.WriteStartElement("UtilizatoriDatabase"); xmlTextWriter.WriteStartElement("Utilizator"); // write next element xmlTextWriter.WriteStartElement("Utilizator_ID", ""); xmlTextWriter.WriteString("123"); xmlTextWriter.WriteEndElement(); // write other element xmlTextWriter.WriteStartElement("Utilizator_password", ""); xmlTextWriter.WriteString("Aladin"); xmlTextWriter.WriteEndElement(); xmlTextWriter.WriteEndElement(); xmlTextWriter.WriteStartElement("Utilizator"); // write next element xmlTextWriter.WriteStartElement("Utilizator_ID", ""); xmlTextWriter.WriteString("456"); xmlTextWriter.WriteEndElement(); // write other element xmlTextWriter.WriteStartElement("Utilizator_password", ""); xmlTextWriter.WriteString("Qwerty"); xmlTextWriter.WriteEndElement(); xmlTextWriter.WriteEndElement(); xmlTextWriter.WriteEndDocument(); xmlTextWriter.Close(); System.Console.WriteLine("XML was generated"); }