Json.NET - convert JSON to XML and remove XML version, encoding? - c#

http://james.newtonking.com/projects/json/help/
How come when I use "DeserializeXmlNode" and my JSON gets converted to an XML document
then convert my XML document into a string like this
string strXML = "";
StringWriter writer = new StringWriter();
xmlDoc.Save(writer);
strXML = writer.ToString();
It includes
<?xml version="1.0" encoding="utf-16"?>
I did not add this, how do I remove it?

an XML without that line is not a valid XML file!
that line is called the XML Declaration
as an example, check out the OData XML from Netflix on Catalog Titles, can you see that first line?
http://odata.netflix.com/Catalog/Titles

Use XmlWriter with StringBuilder instead of StringWriter
var strXML = "";
var writer = new StringBuilder();
var settings = new System.Xml.XmlWriterSettings() { OmitXmlDeclaration = true};
var xmlWriter = System.Xml.XmlWriter.Create(strXML, settings);
xmlDoc.Save(xmlWriter);
strXML = writer.ToString();

Related

Converting XML to UTF-8 using C#

I have written below code to convert XML file to UTF-8 format file, it is working as excepted but issue is header is concatenating with body text instead of writing in separate line. I need utf8 in seperate line but file.writealltext will not accept more than 3 arguments/parameters. Any help appreciated.
string path = #"samplefile.xml";
string path_new = #"samplefile_new.xml";
Encoding utf8 = new UTF8Encoding(false);
Encoding ansi = Encoding.GetEncoding(1252);
string xml = File.ReadAllText(path, ansi);
XDocument xmlDoc = XDocument.Parse(xml);
File.WriteAllText(
path_new,
#"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""true"">" + xmlDoc.ToString(),
utf8
);
No need to use any API other than LINQ to XML. It has all means to deal with XML file encoding, prolog, BOM, indentation, etc.
void Main()
{
string outputXMLfile = #"e:\temp\XMLfile_UTF-8.xml";
XDocument xml = XDocument.Parse(#"<?xml version='1.0' encoding='utf-16'?>
<root>
<row>some text</row>
</root>");
XDocument doc = new XDocument(
new XDeclaration("1.0", "utf-8", null),
new XElement(xml.Root)
);
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.IndentChars = "\t";
// to remove BOM
settings.Encoding = new UTF8Encoding(false);
using (XmlWriter writer = XmlWriter.Create(outputXMLfile, settings))
{
doc.Save(writer);
}
}

Change xslCompiledTransform output encoding while transformig

I am Converting my XML to XSLT by using xslCompiledTransform and when it transforms it changes the output result encoding which is UTF-16 (by default)
and when I try to change its or encoding it prompts the error that this property is read-only you can't change it!
I also try xmlWriter and xmlWriterSettings and memory Stream and other solutions but nothing works for me and for the reference I am adding code snippet
public static StringBuilder TransformXml(ProcessorConfigElement configSettings, StringBuilder xml, ILog logger)
{
//Perform transformation...
StringBuilder newXmlBuilder = new StringBuilder(xml.Length);
XslCompiledTransform requiredXslt = new XslCompiledTransform();
requiredXslt.Load(configSettings.XsltPath, XsltSettings.TrustedXslt, new XmlUrlResolver());
// I tried this trick also but all in vain
// Encoding wind1252 = Encoding.GetEncoding(1252);
// XmlWriterSettings xmlSettings = new XmlWriterSettings();
// xmlSettings.Encoding = wind1252;
// xmlSettings.ConformanceLevel = ConformanceLevel.Fragment;
// xmlSettings.OmitXmlDeclaration = true;
// XmlWriter writer = XmlWriter.Create(newXmlBuilder, xmlSettings);
StringBuilder s = new StringBuilder();
using (TextWriter newXmlWriter = StringWriter.Create(newXmlBuilder))
{
if (!string.IsNullOrEmpty(configSettings.Delimiter))
{
XsltArgumentList argsList = new XsltArgumentList();
argsList.AddParam("delimiter", "", configSettings.Delimiter);
// here is the actual problem when it transforms its create a mess and converting pound symbol and other symbols as diamond special character (encoding issue.)
requiredXslt.Transform(GetElement(xml.ToString()), argsList, newXmlWriter);
}
else
{
requiredXslt.Transform(GetElement(xml.ToString()), null, newXmlWriter);
}
}
logger.Info("XSLT applied successfully");
//replace string after transformation to validate and write to file
xml = newXmlBuilder;
return xml;
}
I want to use the desired UTF encoding while transforming it to XSLT, anyone?
As Martin Honnen already pointed out, if XSLT already has output declaration along the following line:
XSLT
<xsl:output indent="yes" method="xml" encoding="utf-8"/>
Here is c# that picks it up from the XSLT file via xslt.OutputSettings parameter:
c#
void Main()
{
const string SOURCEXMLFILE = #"e:\Temp\UniversalShipment.xml";
const string stylesheet = #"e:\Temp\UniversalShipment.xslt";
const string OUTPUTXMLFILE = #"e:\temp\UniversalShipment_output.xml";
bool paramXSLT = false;
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load(stylesheet, XsltSettings.TrustedXslt, new XmlUrlResolver());
// Load the file to transform.
XPathDocument doc = new XPathDocument(SOURCEXMLFILE);
XsltArgumentList xslArg = new XsltArgumentList();
if (paramXSLT)
{
// Create a parameter which represents the current date and time.
DateTime d = DateTime.Now;
xslArg.AddParam("date", "", d.ToString());
}
using (XmlWriter writer = XmlWriter.Create(OUTPUTXMLFILE, xslt.OutputSettings))
{
xslt.Transform(doc, xslArg, writer);
}
}

Illegal characters in path error in XML Deserilation

I have got XML response from my client. I can't deserialize the XML as string, it throws an Illegal characters in path error. So now I save the file in temp folder and retrieve that. Is it possible to do the deserialize without saving the XML file first?
string xml = Post();
XmlSerializer deserializer = new XmlSerializer(typeof(Envelope));
TextReader reader = new StreamReader(xml); <-- Illegal characters in path error -->
object obj = deserializer.Deserialize(reader);
Envelope XmlData = (Envelope)obj;
reader.Close();
Edit 1 -
XmlSerializer serializer = new XmlSerializer(typeof(Envelope));
using (TextWriter writer = new StreamWriter(xml)) <-- StringWriter is Possible here? -->
{
serializer.Serialize(writer, XmlData);
}
Instead of a StreamReader, use a StringReader, that takes a string as constructor parameter.
TextReader reader = new StringReader(xml);
For writing, use this:
string output;
XmlSerializer serializer = new XmlSerializer(typeof(Envelope));
using (TextWriter writer = new StreamWriter(xml)) <-- StringWriter is Possible here? -->
{
serializer.Serialize(writer, XmlData);
output = writer.ToString();
}

XmllSerializer.Serialize and returning raw xml

I am using an XmlSerializer to serialize an object to xml. After the object gets serialized, I end up with something like...
<?xml version="1.0" encoding="utf-8"?>
<xml xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
</xml>
I need to have it return simply...
<xml></xml>
Is there a way to serialize xml without the extra information? I realize strictly proper xml requires these additional elements, but I need the simpler form as I am appending these xml strings together to form a larger xml blob.
UPDATE
I was able to get the following code to work...
public static string Serialize(object o)
{
XmlWriterSettings xws = new XmlWriterSettings();
xws.OmitXmlDeclaration = true;
xws.Indent = true;
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add(String.Empty, String.Empty);
StringBuilder sb = new StringBuilder();
XmlWriter xmlw = XmlWriter.Create(sb, xws);
XmlSerializer serializer = new XmlSerializer(o.GetType());
serializer.Serialize(xmlw, o, ns);
xmlw.Flush();
return sb.ToString();
}
In C#, you can do something like this:
XmlSerializer serializer = new XmlSerializer(typeof(object));
StringWriter stringWriter = new StringWriter();
using (XmlWriter writer = XmlWriter.Create(stringWriter, new XmlWriterSettings() { OmitXmlDeclaration = true }))
{
serializer.Serialize(writer, this, new XmlSerializerNamespaces() { "",""});
}
string xmlText = stringWriter.ToString();
Explanation:
OmitXmlDeclaration = true makes it remove the declaration.
new XmlSerializerNamespaces() { "",""} removes the namespaces.

Decode & from & from XML with ToString()

How do I get the code below to return the desired output. This is just plain csharp in a main, no ASP.NET.
//Desired output: <amp>Before & After</amp>
//instead of
//Current output: <amp>Before & After</amp>
static void Main(string[] args)
{
string amp = "Before & After";
XmlDocument doc = new XmlDocument();
StringBuilder sb = new StringBuilder();
StringWriter stringWriter = new StringWriter(sb);
XmlTextWriter xmlWriter = new XmlTextWriter(stringWriter);
xmlWriter.WriteStartElement("amp");
xmlWriter.WriteString(amp);
xmlWriter.WriteEndElement();
global::System.Windows.Forms.MessageBox.Show(sb.ToString());
}
Here is how I solved the particular problem.
string amp = "Before & After";
XmlDocument doc = new XmlDocument();
StringBuilder sb = new StringBuilder();
StringWriter stringWriter = new StringWriter(sb);
XmlTextWriter xmlWriter = new XmlTextWriter(stringWriter);
xmlWriter.WriteStartElement("amp");
xmlWriter.WriteString(amp);
xmlWriter.WriteEndElement();
StringReader valueStringReader = new StringReader(sb.ToString());
XmlTextReader valueXmlReader = new XmlTextReader(valueStringReader);
valueXmlReader.MoveToContent();
global::System.Windows.Forms.MessageBox.Show(valueXmlReader.ReadString());
If you want invalid XML create it yourself.
string amp = "Before & After";
// don't really do this, it's very wrong
StringBuilder sb = new StringBuilder();
sb.Append("<amp>");
sb.Append(amp);
sb.Append("</amp>");
Console.WriteLine(sb);
But this is NOT valid xml, so don't do it. If you want to use XML, you need to use valid XML. Your original code sample is correct.
If you want something that is more human readable, then don't use XML, use YAML.
http://www.yaml.org/
amp: Before & After
The desired output is not valid XML. The current output is identical to a CDATA section in that Before & After and <![CDATA[Before & After]]> are simply two different ways of escaping an ampersand so that you can have valid XML. In either case, if you use any XML parser to read the content of the <amp> tag, it will return Before & After.
You could put your text into a CDataSection - see XmlDocument.CreateCDataSection.
Here is how I solved the particular problem.
string amp = "Before & After";
XmlDocument doc = new XmlDocument();
StringBuilder sb = new StringBuilder();
StringWriter stringWriter = new StringWriter(sb);
XmlTextWriter xmlWriter = new XmlTextWriter(stringWriter);
xmlWriter.WriteStartElement("amp");
xmlWriter.WriteString(amp);
xmlWriter.WriteEndElement();
StringReader valueStringReader = new StringReader(sb.ToString());
XmlTextReader valueXmlReader = new XmlTextReader(valueStringReader);
valueXmlReader.MoveToContent();
global::System.Windows.Forms.MessageBox.Show(valueXmlReader.ReadString());

Categories