How can I save an XmlDocument without the XML Prolog? - c#

While working on csproj files as xml files (xmlDocument) somehow after saving, encoding changes from UTF-8 to ASCII. The only solution I figured out is to use StreamWriter with declared encoding. But this adds a header to file.
Questions:
1. Do you know any other solution to this problem?
2. Can I leave changed csproj?
StreamReader sr = new StreamReader(projectFilePath, Encoding.UTF8);
XmlDocument xmlCsproj = new XmlDocument();
xmlCsproj.Load(sr);
sr.Close();
// Working on XML nodes (replace references paths)
StreamWriter sw= new StreamWriter(projectFilePath, false, Encoding.UTF8);
xmlCsproj.Save(sw);
sw.Close();

This should work?
var writer = new XmlTextWriter(projectFilePath, Encoding.UTF8)
{
Formatting = Formatting.Indented
};
xmlCsproj.WriteContentTo(writer);
writer.Close();

First of all, UTF8 character values are identical to US-ASCII values in the range 0-127. There's no way to say if a csproj file was saved as US-ASCII or UTF8 unless the file contained non-English text, like non-English file paths. Is that what happened here? Even so, XmlDocument.Save should have used UTF8 by default.
You can control how an XmlDocument is written to a stream or file using an XmlWriter and XmlWriterSettings. In this case, you need to set the OmitXmlDeclaration :
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
var writer = XmlWriter.Create(projectFilePath, settings);
xmlCsproj.Save(sw);
sw.Close();
The default encoding is UTF8. To change it, you can set the XmlWriterSettings.Encoding to a different value

Related

Flat xml file c#

I'm wrting xml file with UTF-8 (without Bom) encoding as follow:
xmldecl.Encoding = "UTF-8";
dataDoc.InsertBefore(xmldecl, root);//dataDoc is XmlDocument object
using (var writer = new XmlTextWriter(targetPath, new UTF8Encoding(false)))
{
dataDoc.Save(writer);
}
My "problem" is the file is saved in one line instead of xml formatting,
I.e if i have the following xml:
<ElementA>
<ElementB/>
</ElementA>
With my code the xml file will be:
<ElementA><ElementB/></ElementA>
Instead of xml format.
How can i solve it?
*I'm try to open the file with Notepad++
Thanks.
XmlTextWriter has a property Formatting to define the way the output is written:
using (var writer = new XmlTextWriter(targetPath, new UTF8Encoding(false)))
{
writer.Formatting = Formatting.Indented;
dataDoc.Save(writer);
}

XDocument automatically indents files on parse and does not remove indent on save

I am seeking a help regarding file saving of XML file using XDocument (NOT XMLDocument).
So I have a xml file that does not have indentation (in fact it is 1 line). When I read this to XDocument using XDocument.Parse (after reading and storing string using StreamReader), the resulting XDocument is indented.
Alright, I thought it will be fine as long as if I can save it back to the file without indentation. However, even though I have
XmlWriterSettings writerSettings = new XmlWriterSettings();
writerSettings.NewLineOnAttributes = false;
writerSettings.NewLineHandling = NewLineHandling.None;
writerSettings.Indent = false;
and pass that in when I create XmlWriter
using (var writer = XmlWriter.Create(u.ToFileSystemPath(), settings))
{
xd.Save(writer);
}
The resulting XML file still has indentation. When I am debugging on Visual studio, I noticed that the writer is a class XmlWellFormedWriter. Does this have something to do with my result? Any help would be appreciated.
Thank you.
SaveOptions are available on Save() as well as ToString().
string xmlstring =
#"<Top>
<First>1</First>
<Second>Dude</Second>
<Third>Now</Third>
</Top>";
XDocument doc = XDocument.Parse(xmlstring);
doc.Save(#"C:\temp\noIndet.xml", SaveOptions.DisableFormatting);
// string noIndent = doc.ToString(SaveOptions.DisableFormatting);
Output:

XmlTextWriter serialize Object to XML element

I would like to perform object serialization to only one branch in an existing XML file. While reading by using:
RiskConfiguration AnObject;
XmlSerializer Xml_Serializer = new XmlSerializer(typeof(RiskConfiguration));
XmlTextReader XmlReader = new XmlTextReader(#"d:\Projects\RiskService\WCFRiskService\Web.config");
XmlReader.ReadToDescendant("RiskConfiguration");
try
{
AnObject = (RiskConfiguration)Xml_Serializer.Deserialize(XmlReader);
AnObject.Databases.Database[0].name = "NewName";
}
finally
{
XmlReader.Close();
}
It is possible, I do not know how to edit the object again performed it can save the file without erasing other existing elements in an XML file. Can anyone help me?
I found a way to display the desired item serialization. How do I go now instead of the paste to the original element in XML?
StringWriter wr = new StringWriter();
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.OmitXmlDeclaration = true;
settings.Encoding = System.Text.Encoding.Default;
using (XmlWriter writer = XmlWriter.Create(wr, settings))
{
XmlSerializerNamespaces emptyNamespace = new XmlSerializerNamespaces();
emptyNamespace.Add(String.Empty, String.Empty);
Xml_Serializer.Serialize(writer, AnObject, emptyNamespace);
MessageBox.Show(wr.ToString());
}
First of all, you should stop using new XmlTextReader(). That has been deprecated since .NET 2.0. Use XmlReader.Create() instead.
Second, XML is not a random-access medium. You can't move forward and backwards, writing into the middle of the file. It's a text-based file.
If you need to "modify" the file, then you'll need to write a new version of the file. You could read from the original file, up to the point where you need to deserialize, writing the nodes out to a new version of the file. You could then deserialize from the original file, modify the objects, and serialize out to the new version. You could then continue reading from the original and writing the nodes out to the new version.

C# XML can't load indent value

The code below is causing the " Data at the root level is invalid. Line 1, position 1. "
I like to keep the code indent(linebreak) but keep having the problem as mentioned above. I could use the TextReader to load the xml but it will remove the indent which i don't like it. If you know how to fix problem please let me know. Thanks
public XmlDocument MYXML()
{
XmlWriterSettings wSettings = new XmlWriterSettings();
wSettings.Indent = false;
wSettings.OmitXmlDeclaration = false;
MemoryStream ms = new MemoryStream();
XmlWriter xw = XmlWriter.Create(ms, wSettings);// Write Declaration
xw.WriteStartDocument();
// Write the root node
xw.WriteStartElement("Library");
// Write the books and the book elements
xw.WriteStartElement("Book");
xw.WriteStartAttribute("BookType");
xw.WriteString("Hardback");
xw.WriteEndAttribute();
xw.WriteStartElement("Title");
xw.WriteString("Door Number Three");
xw.WriteEndElement();
xw.WriteStartElement("Author");
xw.WriteString("O'Leary, Patrick");
xw.WriteEndElement();
xw.WriteEndElement();
xw.WriteEndElement();
// Close the document
xw.WriteEndDocument();
// Flush the write
xw.Flush();
Byte[] buffer = new Byte[ms.Length];
buffer = ms.ToArray();
string xmlOutput = System.Text.Encoding.UTF8.GetString(buffer);
//The next 3 line works fine but it will remove the Indent from the XmlWriterSettings
//TextReader tr = new StreamReader(ms);
//ms.Seek(0, SeekOrigin.Begin);
//xmlOutput = tr.ReadToEnd() + "";
//Can't nload the xmlOutput from buffer
XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml(xmlOutput);
return xmldoc;
}
The XmlWriter is writing a utf-8 byte-order mark to the stream. Encoding.UTF8.GetString doesn't account for this (since it's only supposed to occur in files) so the first character of the string becomes an invalid, unprintable character, which is what XmlDocument.LoadXml chokes on.
EDIT: Since you said you want to create an XmlDocument so you can reuse it, I recommend one of the following:
If using .Net 3.5 or newer, use XDocument which is much easier to use (I recommend this).
Create the XmlDocument directly by constructing nodes and adding them to the tree.
Create the XmlDocument directly from the writer by using XPathNavigator (XmlWriter writer = doc.CreateNavigator.AppendChild())
Note that you can't easily add insignificant whitespace to an XmlDocument. Using XDocument and writing it to the output using doc.Save(Response.Output) is by far the easiest option if you want to have nicely formatted output.

Unknown chr being output after XSL Compiled Transform

my code is outputting some weird character at the very start of my XSLT output XML and neither Visual Studio 2008 or notepad show it up. But it's definitely there because VS lets me delete it and will then auto-format the XML properly. How do I stop this? Here's my code:
// create the readers for the xml and xsl
XmlReader reader = XmlReader.Create(
new StringReader(LoadFileAsString(MapPath(xslPath)))
);
XmlReader input = XmlReader.Create(
new StringReader(LoadFileAsString(MapPath(xmlPath)))
);
// create the xsl transformer
XslCompiledTransform t = new XslCompiledTransform(true);
t.Load(reader);
// create the writer which will output the transformed xml
StringBuilder sb = new StringBuilder();
//XmlWriterSettings tt = new XmlWriterSettings();
//tt.Encoding = Encoding.Unicode;
XmlWriter results = XmlWriter.Create(new StringWriter(sb));//, tt);
// write the transformed xml out to a stringbuilder
t.Transform(input, null, results);
// return the transformed xml
WriteStringAsFile(MapPath(outputXmlPath), sb.ToString());
public static string LoadFileAsString(string fullpathtofile)
{
string a = null;
using (var sr = new StreamReader(fullpathtofile))
a = sr.ReadToEnd();
return a;
}
public static void WriteStringAsFile(string fullpathtofile, string content)
{
File.WriteAllText(fullpathtofile, content.Trim(), Encoding.Unicode);
}
That thing at the beginning of your XML output document is most likely a byte-order-mark or BOM, which indicates whether the bytes in your Unicode output are in big-endian or little-endian order.
This BOM might be useful for consumers of your XML document; however, in some cases it might lead to problems and then it is better not to create it.
You can specify whether a BOM is created using the Encoding specified via XmlWriterSettings:
XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
xmlWriterSettings.Encoding = new UTF8Encoding(false);
The code above will create your document using UTF8 encoding. This is most likely what you want to have unless your consuming system explicitly asks for UTF16/Unicode encoding or you are dealing with Asian character.
To create a UTF16/Unicode encoded document use UnicodeEncoding with the second parameter set to false:
XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
xmlWriterSettings.Encoding = new UnicodeEncoding(false, false);

Categories

Resources