I have a standard XML document, which looks like a 'tree'. I add some nodes and save changes:
XmlDocument doc = new XmlDocument();
doc.Load(filename);
...
doc.PreserveWhitespace = true;
XmlTextWriter writer = new XmlTextWriter(filename, Encoding.Default);
doc.WriteTo(writer);
writer.Close();
and after that xml document stretch into one line. How to add line feeds?
After you construct the XmlTextWriter, but before you call doc.WriteTo(writer);, insert this line:
writer.Formatting = Formatting.Indented;
Related
I am writing a XML using XmlDocument, I need a element or attribute like shown below
The element or attribute required is <?Validversion="1" ?>
how to create using xmldocument or xmlwriter.
// to create <?Validversion="1" ?>
XmlDocument aDoc = new XmlDocument();
aDoc.CreateXmlDeclaration("1.0", "utf-16", null);
XmlCDataSection aDataSec =aDoc.CreateCDataSection("?Version = 2");
aDoc.AppendChild(aDataSec);
aDoc.Save("c:\\vector.xml");
You are looking for XmlDocument.CreateProcessingInstruction and not CDATA section:
var document = new XmlDocument();
document.AppendChild(document.CreateXmlDeclaration("1.0", "utf-16", null));
var piNode = document.CreateProcessingInstruction("Version", "=\"2\"");
document.AppendChild(pi);
Side note: don't forget to AppendChild newly created node.
How To append in the XMl file using XMLWriter class
XmlWriterSettings xmlFragments = new XmlWriterSettings();
xmlFragments.Indent = true ;
XmlTextWriter xWriter = new XmlTextWriter(_logFilePath, null);
xWriter.WriteStartDocument();//starting document
xWriter.WriteStartElement("values");//starting parent node
xWriter.WriteStartElement("values1");//1st child node
xWriter.WriteAttributeString("id", "200");//attributes of child node
xWriter.WriteAttributeString("name", "ABCD");
xWriter.WriteString("ISO Company");//innertext
xWriter.WriteEndElement();
xWriter.WriteStartElement("num");//2nd child node
xWriter.WriteAttributeString("more", "500");
xWriter.WriteAttributeString("less", "101");
xWriter.WriteString("numeric");
xWriter.WriteEndElement();
xWriter.WriteStartElement("r", "runnnig", "");//3rd child node
xWriter.WriteAttributeString("fast", "500km");
xWriter.WriteAttributeString("slow", "10km");
xWriter.WriteString("killometers");
xWriter.WriteEndElement();
xWriter.WriteStartElement("character");
xWriter.WriteAttributeString("char", "a");
xWriter.WriteAttributeString("another", "b");
xWriter.WriteEndElement();
xWriter.WriteEndElement();
xWriter.WriteEndDocument();
xWriter.Close();
I am trying to load xml file in xmlwriter but this class overwrite the previous tag but i dont want to overwrite the tag.
It can be solved by XmlDocument.
XmlDocument doc = new XmlDocument();
doc.Load("test.xml");
XmlElement el = doc.CreateElement("child");
el.InnerText = "This row is being appended to the end of the document.";
doc.DocumentElement.AppendChild(el);
doc.Save("test.xml");
check here for more options.
I want to convert one xml file into another xml file using xslt.here, i can able to pass the input document to the XPathDocument and also save the output file in disk by passing outfile into XmlTextWriter.
But now my problem is... i have my input is in string format and i also want output as a string.Instead of passing the location of the input file, i want to pass string that contains the xml data.
so i have to pass string object into xpathDoccument in someway and also get the resultant xml file as a string.Instead of save the output as a file,i want output as a string.
XPathDocument xpathDoc = new XPathDocument("C:\\InputXml.xml");
XslCompiledTransform xslt = new XslCompiledTransform();
string xsltFile = "C:\\conversion.xslt";
xslt.Load(xsltFile);
string outputFile = "C:\\myHtml.html";
XmlTextWriter writer = new XmlTextWriter(outputFile, null);
xslt.Transform(xpathDoc, null, writer);
writer.Close();
Please guide me to get out of this issue...
XPathDocument accepts TextReader. You can give a stream as new XPathDocument(new StringReader(xmlstring)). Similarly XmlTextWriter accepts TextWriter. So you can pass a StringWriter.
--edit--
var sw = new StringWriter();
XmlTextWriter writer = new XmlTextWriter(sw);
xslt.Transform(xpathDoc, null, writer);
var str= sw.ToString();
Try this,
XslTransform xTrans = new XslTransform();
xTrans.Load(nodeXsltPath); //xsl file path
XmlDocument input= new XmlDocument();
XmlDocument output= new XmlDocument();
input.LoadXml(xmlString); /* Xml string to be loaaded */
output.Load(xTrans.Transform(input,null,new XmlUrlResolver()));
output.Save(filePathtoSave);
I have a class that is marked with DataContract attributes and I would like to create an XDocument from objects of that class. Whats the best way of doing this?
I can do it by going via an XmlDocument but this seems like an unnecessary step.
You can create an XmlWriter directly into the XDocument:
XDocument doc = new XDocument();
using (var writer = doc.CreateWriter())
{
// write xml into the writer
var serializer = new DataContractSerializer(objectToSerialize.GetType());
serializer.WriteObject(writer, objectToSerialize);
}
Console.WriteLine(doc.ToString());
this is how i do it, which gives clean xml without all the namespace stuff in it,
XDocument xdoc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"));
using (var writer = xdoc.CreateWriter())
{
System.Xml.Serialization.XmlSerializer x =
new System.Xml.Serialization.XmlSerializer(objecttoserialize.GetType());
x.Serialize(writer, objecttoserialize);
}
Debug.WriteLine(xdoc.ToString());
I am editing csproj files with Linq-to-XML and need to save the XML without the <?XML?> header.
As XDocument.Save() is missing the necessary option, what's the best way to do this?
You can do this with XmlWriterSettings, and saving the document to an XmlWriter:
XDocument doc = new XDocument(new XElement("foo",
new XAttribute("hello","world")));
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
StringWriter sw = new StringWriter();
using (XmlWriter xw = XmlWriter.Create(sw, settings))
// or to write to a file...
//using (XmlWriter xw = XmlWriter.Create(filePath, settings))
{
doc.Save(xw);
}
string s = sw.ToString();
A simpler solution than the accepted answer is to use XDocument.ToString() to get the XML text without the header.
Example:
// Load the file
XDocument xDocument = XDocument.Load(fileName);
// Edit the XML...
// Save the edited XML text to file
File.WriteAllText(fileName, xDocument.ToString());