I am serializing objects to XML with the following code:
public static string SerializeToString<T>(T objectToBeSerialized, string defaultNamespace)
{
StringBuilder stringBuilder = new StringBuilder();
XmlWriterSettings xmlSettings = new XmlWriterSettings()
{
CloseOutput = true,
Indent = true,
OmitXmlDeclaration = true
};
using (XmlWriter xmlWriter = XmlWriter.Create(stringBuilder, xmlSettings))
{
XmlSerializer serializer = new XmlSerializer(typeof(T), defaultNamespace);
serializer.Serialize(xmlWriter, objectToBeSerialized);
return stringBuilder.ToString();
}
}
I am already setting the default namespace ("http://schemas.somecompany.com/online/someservice/sync/2008/11"); however, my outputs still contain the default "xmlns:xsi" and "xmlns:xsd
<RootTag ***xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"*** xmlns="http://schemas.somecompany.com/online/someservice/sync/2008/11">
<SomeTag>
<More>false</More>
</SomeTag>
</RootTage>
How can I get rid of them?
XmlSerializer: remove unnecessary xsi and xsd namespaces
Related
I have this class
public class EnvioDTE
{
[XmlAttribute]
public string version { get; set; }
public EnvioDTE()
{
this.version = "1.0";
}
}
I am serializing it to XML with the following code:
EnvioDTE envioDTE = new EnvioDTE();
string xml = "";
var serializer = new XmlSerializer(typeof(EnvioDTE));
var settings = new XmlWriterSettings
{
Indent = true,
NewLineChars = "\n",
OmitXmlDeclaration = false,
Encoding = Encoding.GetEncoding("ISO-8859-1")
};
using (var stream = new MemoryStream())
{
using (var xmlWriter = XmlWriter.Create(stream, settings))
{
serializer.Serialize(xmlWriter, this);
xml = Encoding.GetEncoding("ISO-8859-1").GetString(stream.ToArray());
}
}
Console.WriteLine(xml);
Which is giving me the following XML:
<?xml version="1.0" encoding="iso-8859-1"?>
<EnvioDTE version="1.0">
</EnvioDTE>
What do I need to add to my code so that I get the following attributes?
<?xml version="1.0" encoding="iso-8859-1"?>
<EnvioDTE xmlns="http://www.sii.cl/SiiDte" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sii.cl/SiiDte EnvioDTE_v10.xsd" version="1.0">
</EnvioDTE>
The order of the attributes is important for me.
I tried by making two changes:
Change #1
[XmlRoot(Namespace = "http://www.sii.cl/SiiDte")] // *** NEW
public class EnvioDTE
{
[XmlAttribute("schemaLocation", Namespace = "http://www.w3.org/2001/XMLSchema-instance")] // *** NEW
public string schemaLocation { get; set; } // *** NEW
[XmlAttribute]
public string version { get; set; }
public EnvioDTE()
{
this.version = "1.0";
}
}
Change #2
EnvioDTE envioDTE = new EnvioDTE();
string xml = "";
var namespaces = new XmlSerializerNamespaces(); // *** NEW
namespaces.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance"); // *** NEW
var serializer = new XmlSerializer(typeof(EnvioDTE));
var settings = new XmlWriterSettings
{
Indent = true,
NewLineChars = "\n",
OmitXmlDeclaration = false,
Encoding = Encoding.GetEncoding("ISO-8859-1")
};
using (var stream = new MemoryStream())
{
using (var xmlWriter = XmlWriter.Create(stream, settings))
{
serializer.Serialize(xmlWriter, this, namespaces); // *** NEW
xml = Encoding.GetEncoding("ISO-8859-1").GetString(stream.ToArray());
}
}
Console.WriteLine(xml);
With those changes I'm getting this:
<?xml version="1.0" encoding="iso-8859-1"?>
<EnvioDTE xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sii.cl/SiiDte EnvioDTE_v10.xsd" version="1.0" xmlns="http://www.sii.cl/SiiDte">
</EnvioDTE>
And that is not the order of attributes I want...
So you are just trying to read out the XML? I would do this:
XmlReader reader = XmlReader.Create(path, settings);
StringBuilder sb = new StringBuilder();
if (reader != null)
{
while (reader.Read())
{
sb.AppendLine(reader.ReadOuterXml());
}
doc = new XmlDocument();
doc.LoadXml(Convert.ToString(sb));
XmlNodeList xmlNodeList;
xmlNodeList = doc.GetElementsByTagName("Whatever your tag name");
for (i = 0; i < xmlNodeList.Count; i++)
{
if (xmlNodeList[i].InnerXml.Length > 0)
{
foo = xmlNodeList[i].InnerXml;
}
}
}
Okay, you are just trying to output? Then do this. I know it's a bit of a hack and there is probably a million ways to do it:
string myXMLstring = "<?xml version=\"1.0\" encoding="iso-8859-1\"?>" +
<EnvioDTE xmlns=\"http://www.sii.cl/SiiDte\"" +
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
"xsi:schemaLocation=\"http://www.sii.cl/SiiDte EnvioDTE_v10.xsd\"" +
"version=\"1.0\">" +
"</EnvioDTE>";
I've escaped the the quotes and built it up as a string. You can even add \r\n anytime you know you need a new line. I hope that helps. Happy coding!
i have some XML files and i have wrote a c# application to check missing elements, nodes and save it back. In my XMLs attributes are use single quotes (ex: <Person name='Nisala' age='25' >). But when saving C# application convert those quotes to double quotes. Then i found following code to save using single quotes
using (XmlTextWriter tw = new XmlTextWriter(file, null))
{
tw.Formatting = Formatting.Indented;
tw.Indentation = 3;
tw.IndentChar = ' ';
tw.QuoteChar = '\'';
xmlDoc.Save(tw);
}
}
but it will append XML declaration there. then i found this code to remove xml declaration
XmlWriterSettings xws = new XmlWriterSettings();
xws.OmitXmlDeclaration = true;
xws.Indent = true;
xws.ConformanceLevel = ConformanceLevel.Fragment;using (XmlWriter xw = XmlWriter.Create(file, xws)){
xmlDoc.Save(xw);
}
then again XML declaration is appending to text. How can i use both of them?
i have tried following code too, but no use of it
XmlWriterSettings xws = new XmlWriterSettings();
xws.OmitXmlDeclaration = true;
xws.Indent = true;
xws.ConformanceLevel = ConformanceLevel.Fragment;
using (XmlTextWriter tw = new XmlTextWriter(file, null))
{
tw.Formatting = Formatting.Indented;
tw.Indentation = 3;
tw.IndentChar = ' ';
tw.QuoteChar = '\'';
using (XmlWriter xw = XmlWriter.Create(tw, xws))
{
xmlDoc.Save(xw);
}
}
The XML declaration is written by calling WriteStartDocument on the XmlWriter implementation. The behaviour of this is can be altered when you use the recommended XmlWriter.Create with XmlWriterSettings.
However, the recommended method doesn't allow you to change the quote character.
The only solution I can think of is to create your own writer, deriving from XmlTextWriter. You would then override WriteStartDocument to prevent any declaration being written:
public class XmlTextWriterWithoutDeclaration : XmlTextWriter
{
public XmlTextWriterWithoutDeclaration(Stream w, Encoding encoding)
: base(w, encoding)
{
}
public XmlTextWriterWithoutDeclaration(string filename, Encoding encoding)
: base(filename, encoding)
{
}
public XmlTextWriterWithoutDeclaration(TextWriter w)
: base(w)
{
}
public override void WriteStartDocument()
{
}
}
And use as you are now:
using (var tw = new XmlTextWriterWithoutDeclaration(file, null))
{
tw.Formatting = Formatting.Indented;
tw.Indentation = 3;
tw.IndentChar = ' ';
tw.QuoteChar = '\'';
xmlDoc.Save(tw);
}
I need to create a human-readable XML file. XmlWriter seems to be almost perfect for this, but I'd like to insert line breaks or, in general, custom whitespace where I want them. Neither WriteRaw nor WriteWhitespace seem to work between the attributes in an element. The latter looks like it should work (This method is used to manually format your document), but throws InvalidOperationException (Token StartAttribute in state Element Content would result in an invalid XML document) if used in place of the comments below.
Is there a workaround for XmlWriter or a third-party XML library that supports this?
Example code (LINQPad-statements ready):
var sb = new StringBuilder();
var settings = new XmlWriterSettings {
OmitXmlDeclaration = true,
Indent = true,
IndentChars = " ",
};
using (var writer = XmlWriter.Create(sb, settings)) {
writer.WriteStartElement("X");
writer.WriteAttributeString("A", "1");
// write line break
writer.WriteAttributeString("B", "2");
// write line break
writer.WriteAttributeString("C", "3");
writer.WriteEndElement();
}
Console.WriteLine(sb.ToString());
Actual result:
<X A="1" B="2" C="3" />
Desired result (B and C may be not aligned with A - that's fine, /> can be left on the line with C):
<X A="1"
B="2"
C="3"
/>
You still has the background StringBuilder - use it. :
var sb = new StringBuilder();
var settings = new XmlWriterSettings {
OmitXmlDeclaration = true,
Indent = true,
IndentChars = " ",
};
using (var writer = XmlWriter.Create(sb, settings)) {
writer.WriteStartElement("X");
writer.WriteAttributeString("A", "1");
writer.Flush();
sb.Append("\r\n");
writer.WriteAttributeString("B", "2");
writer.Flush();
sb.Append("\r\n");
writer.WriteAttributeString("C", "3");
writer.WriteEndElement();
}
Console.WriteLine(sb.ToString());
I am looking on Internet how keep the carriage return from XML data but I could not find the answer, so I'm here :)
The objective is to write in a file the content of a XML data. So, if the value of the node contains some "\r\n" data, the soft need to write them in file in order to create new line, but it doesn't write, even with space:preserve.
Here is my test class:
XElement xRootNode = new XElement("DOCS");
XElement xData = null;
//XNamespace ns = XNamespace.Xml;
//XAttribute spacePreserve = new XAttribute(ns+"space", "preserve");
//xRootNode.Add(spacePreserve);
xData = new XElement("DOC");
xData.Add(new XAttribute("FILENAME", "c:\\titi\\prout.txt"));
xData.Add(new XAttribute("MODE", "APPEND"));
xData.Add("Hi my name is Baptiste\r\nI'm a lazy boy");
xRootNode.Add(xData);
bool result = Tools.writeToFile(xRootNode.ToString());
And here is my process class:
try
{
XElement xRootNode = XElement.Parse(xmlInputFiles);
String filename = xRootNode.Element(xNodeDoc).Attribute(xAttributeFilename).Value.ToString();
Boolean mode = false;
try
{
mode = xRootNode.Element(xNodeDoc).Attribute(xWriteMode).Value.ToString().ToUpper().Equals(xWriteModeAppend);
}
catch (Exception e)
{
mode = false;
}
String value = xRootNode.Element(xNodeDoc).Value;
StreamWriter destFile = new StreamWriter(filename, mode, System.Text.Encoding.Unicode);
destFile.Write(value);
destFile.Close();
return true;
}
catch (Exception e)
{
return false;
}
Does anybody have an idea?
If you want to preserve cr lf in element or attribute content when saving a XDocument or XElement you can do that by using certain XmlWriterSettings, namely NewLineHandling to Entitize:
string fileName = "XmlOuputTest1.xml";
string attValue = "Line1.\r\nLine2.";
string elementValue = "Line1.\r\nLine2.\r\nLine3.";
XmlWriterSettings xws = new XmlWriterSettings();
xws.NewLineHandling = NewLineHandling.Entitize;
XDocument doc = new XDocument(new XElement("root",
new XAttribute("test", attValue),
elementValue));
using (XmlWriter xw = XmlWriter.Create(fileName, xws))
{
doc.Save(xw);
}
doc = XDocument.Load(fileName);
Console.WriteLine("att value: {0}; element value: {1}.",
attValue == doc.Root.Attribute("test").Value,
elementValue == doc.Root.Value);
In that example the value are preserved in the round trip of saving and loading as the output of the sample is "att value: True; element value: True."
Heres a useful link I found for parsing an Xml string with carraige returns, line feeds in it.
howto-correctly-parse-using-xelementparse-for-strings-that-contain-newline-character-in
It may help those who are parsing an Xml string.
For those who can't be bothered to click it says use an XmlTextReader instead
XmlTextReader xtr = new XmlTextReader(new StringReader(xml));
XElement items = XElement.Load(xtr);
foreach (string desc in items.Elements("Item").Select(i => (string)i.Attribute("Description")))
{
Console.WriteLine("|{0}|", desc);
}
I'm using XmlTextWriter and its WriteElementString method, for example:
XmlTextWriter writer = new XmlTextWriter("filename.xml", null);
writer.WriteStartElement("User");
writer.WriteElementString("Username", inputUserName);
writer.WriteElementString("Email", inputEmail);
writer.WriteEndElement();
writer.Close();
The expected XML output is:
<User>
<Username>value</Username>
<Email>value</Email>
</User>
However, if for example inputEmail is empty, the result XML I get as as follows:
<User>
<Username>value</Username>
<Email/>
</User>
Whereas I would expect it to be:
<User>
<Username>value</Username>
<Email></Email>
</User>
What am I doing wrong? Is there a way to achieve my expected result in a simple way using XmlTextWriter?
Your output is correct. An element with no content should be written as <tag/>.
You can force the use of the full tag by calling WriteFullEndElement()
writer.WriteStartElement("Email");
writer.WriteString(inputEmail);
writer.WriteFullEndElement();
That will output <Email></Email> when inputEmail is empty.
If you want to do that more than once, you could create an extension method:
public static void WriteFullElementString(this XmlTextWriter writer,
string localName,
string value)
{
writer.WriteStartElement(localName);
writer.WriteString(value);
writer.WriteFullEndElement();
}
Then your code would become:
writer.WriteStartElement("User");
writer.WriteFullElementString("Username", inputUserName);
writer.WriteFullElementString("Email", inputEmail);
writer.WriteEndElement();
It doesn't fail <Tag/> is just a shortcut for <Tag></Tag>
Your code should be:
using (XmlWriter writer = XmlWriter.Create("filename.xml"))
{
writer.WriteStartElement("User");
writer.WriteElementString("Username", inputUserName);
writer.WriteElementString("Email", inputEmail);
writer.WriteEndElement();
}
This avoids resource leaks in case of exceptions, and uses the proper way to create an XmlReader (since .NET 2.0).
Leaving this here in case someone needs it; since none of the answers above solved it for me, or seemed like overkill.
FileStream fs = new FileStream("file.xml", FileMode.Create);
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
XmlWriter w = XmlWriter.Create(fs, settings);
w.WriteStartDocument();
w.WriteStartElement("tag1");
w.WriteStartElement("tag2");
w.WriteAttributeString("attr1", "val1");
w.WriteAttributeString("attr2", "val2");
w.WriteFullEndElement();
w.WriteEndElement();
w.WriteEndDocument();
w.Flush();
fs.Close();
The trick was to set the XmlWriterSettings.Indent = true and add it to the XmlWriter.
Edit:
Alternatively you can also use
w.Formatting = Formatting.Indented;
instead of adding an XmlWriterSettings.
Tried solving this with another approach, might need optimization.
public class SerializeConfig<T> where T : class
{
public static string Serialize(T type)
{
var settings = new XmlWriterSettings
{
Encoding = Encoding.UTF8,
Indent = true,
OmitXmlDeclaration = true
};
var sb = new StringBuilder();
var serializer = new XmlSerializer(type.GetType());
using (var writer = XmlWriter.Create(sb, settings))
{
serializer.Serialize(writer, type);
}
return sb.ToString().FixXmlClosingTags();
}
}
internal static class InsertStringExtention
{
public static string FixXmlClosingTags(this string xmlString)
{
var sb = new StringBuilder();
var xmlTags = xmlString.Split('\r');
foreach (var tag in xmlTags)
{
if (tag.Contains("/>"))
{
var tagValue = tag.Replace("<", "").Replace("/>", "").Trim();
var firstPart = tag.Substring(0, tag.IndexOf('<'));
var newTag = $"{firstPart}<{tagValue}></{tagValue}>";
sb.Append(newTag);
}
else
{
sb.Append(tag);
}
}
return sb.ToString();
}
}