I serialize a Vehicle object with the following code to serialize the object:
XmlSerializer serializer = new XmlSerializer(typeof(Vehicle));
using (StreamWriter sw = new StreamWriter(RESULTS_FILE_PATH_))
using (XmlWriter writer = new XmlTextWriter(sw))
{
try
{
serializer.Serialize(writer, vehicles[0]);
}
catch (Exception exception)
{
Console.WriteLine("Exception thrown: " + exception);
}
}
The results are as follows:
<?xml version="1.0" encoding="utf-8"?>
<Model xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<modelYear xmlns="urn:configcompare4g.kp.chrome.com">2014</modelYear>
<subdivisionName xmlns="urn:configcompare4g.kp.chrome.com">Buick</subdivisionName><modelId
</Model>
I need the format to be like this:
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ModelArrayElement xmlns="urn:configcompare4g.kp.chrome.com">
<model>
<modelYear>2014</modelYear>
<subdivisionName>Buick</subdivisionName>
</model>
</ModelArrayElement>
</S:Body>
</S:Envelope>
Does anyone have any suggestions on how I can produce the proper format?
For such a serialization need to use SoapFormatter.
Detailed information can be found in the description on MSDN
(SoapFormatter).
If your XML output is non-standard and it will be difficult to use any default formatter, I would think about using some templating engine, like RazorEngine, so it would be something like:
string template =
#"<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">"+
"<S:Body>"+
" <ModelArrayElement xmlns="urn:configcompare4g.kp.chrome.com">"+
" <model>"+
" <modelYear>#Model.modelYear</modelYear>"+
" <subdivisionName>#Model.subdivisionName</subdivisionName>"+
" </model>"+
" </ModelArrayElement>"+
"</S:Body>"+
"</S:Envelope>";
var model = vehicles[0];
string result = Razor.Parse(template, model);
This gives a complete control of output and allows to make more complex logic based on loops, conditionals etc.
Related
is there a way to force the XML Deserialize to convert only to an exact matching class object and throw an exception (or error) if it does not match exactly?
The following code will successfully import all 3 xml files:
using System.IO;
using System.Windows;
using System.Xml.Serialization;
namespace XmlDeserialize
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
Person data;
XmlSerializer xs = XmlSerializer.FromTypes(new[] { typeof(Person) })[0];
using (StreamReader reader = new StreamReader(Directory.GetCurrentDirectory() + "\\exact.xml"))
{
data = (Person)xs.Deserialize(reader);
}
using (StreamReader reader = new StreamReader(Directory.GetCurrentDirectory() + "\\reduced.xml"))
{
data = (Person)xs.Deserialize(reader);
}
using (StreamReader reader = new StreamReader(Directory.GetCurrentDirectory() + "\\extended.xml"))
{
data = (Person)xs.Deserialize(reader);
}
}
}
public class Person
{
public string Name;
public int Age;
}
}
exact.xml
<?xml version="1.0" encoding="utf-8"?>
<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Name>MyName</Name>
<Age>9999</Age>
</Person>
The exact.xml fits exactly and therefore it should be possible to deserialized it without any problem
reduced.xml
<?xml version="1.0" encoding="utf-8"?>
<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Name>MyName</Name>
</Person>
To be honest, I expected that I will receive an error by using reduced.xml, but it will be deserialized without a problem. I thought that if "Age" is not nullable (int?) I will receive an error and not the default value of an int. Spezially when keeping in mind that the following code will lead to a compiler error.
int x;
MessageBox.Show("x: " + x); //Error: Use of unassigned local variable 'x'
Therefore the successful deserialization is strange for me but somehow explainable....
But what I completely cannot understand is that fact that I do not receive an error when using the following xml file:
extended.xml
<?xml version="1.0" encoding="utf-8"?>
<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Name>MyName</Name>
<Age>9999</Age>
<Address>MyAddress</Address>
</Person>
The deserialization completely ignores that he is not able to assign "Address" to any class property....
Is it possible to force an exact parsing?
Best for me would be to receive an error by using reduced.xml and extended.xml but at least when using extended.xml I should receive an error....
BR,
MUT
I am writing a routine in C# to validate an Xml file using XmlDocument. Everything seems to be fine except something that i cant understand.
my xml:
<?xml version="1.0" encoding="utf-8"?>
<bdo_fosfec:RegistrosPagosElement xsi:type="bdo_fosfec:RegistrosPagos"
xmlns:bdo_fosfec="http://asocajas.hp.com/bdo_fosfec"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<registro54 xsi:type="bdo_fosfec:Registro54">
<registro82 xsi:type="bdo_fosfec:Registro82">
...
</registro82>
</registro54>
<registro54 xsi:type="bdo_fosfec:Registro54">
<registro82 xsi:type="bdo_fosfec:Registro82">
...
</registro82>
</registro54>
</bdo_fosfec:RegistrosPagosElement>
and the xsd:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns="http://asocajas.hp.com/bdo_fosfec" xmlns:tns1="http://asocajas.hp.com/bdo_fosfec" xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://asocajas.hp.com/bdo_fosfec">
<xsd:annotation>
<xsd:documentation>BOMORIGIN::platform:/resource/bdo_Fosfec/Business%20Objects/bdo_Fosfec.bom#_rs4Sa9itEeSR9aIqvSWzoQ</xsd:documentation>
</xsd:annotation>
...
</xsd:schema>
here is my code to validate my xml against xsd,
XmlDocument xml = new XmlDocument();
xml.Schemas.Add("http://asocajas.hp.com/bdo_fosfec", PathFileXSD);
//load my xml
xml.LoadXml(stringXml);
//event handler to manage the errors from XmlDocument object
ValidationEventHandler veh = new ValidationEventHandler(ErrorValidacion);
//validate my xml
xml.Validate(veh);
and the event handler ErrorValidacion will show me the error
private void ErrorValidacion(object sender, ValidationEventArgs e)
{
string concat = string.Empty;
switch (e.Severity)
{
case XmlSeverityType.Error:
concat = string.Format("Error: {0}", e.Message);
break;
case XmlSeverityType.Warning:
concat = string.Format("Warning {0}", e.Message);
break;
}
}
When run my program the error msj is:
This is an invalid xsi:type 'http://asocajas.hp.com/bdo_fosfec:RegistrosPagos'.
The thing is.. why this message?, The xsi:type for my xml isn't
http://asocajas.hp.com/bdo_fosfec:RegistrosPagos
The xsi:type for my xml is
xsi:type="bdo_fosfec:RegistrosPagos"
Where does the xsi:type http://asocajas.hp.com/bdo_fosfec:RegistrosPagos come from?
So, How can I solve this issue without having to modify the xml? (Because the xml is based on an xslt)
The error message is saying that the XSD does not include a declaration for RegistrosPagos in the http://asocajas.hp.com/bdo_fosfec namespace.
It is using the expanded form of the namespace to report the error instead of the namespace prefix because the namespace prefix is arbitrary and insignificant by definition. This is entirely proper.
In fact, the XSD you've posted does not include a RegistrosPagos in the http://asocajas.hp.com/bdo_fosfec namespace.
See also: How to restrict the value of an XML element using xsi:type in XSD?
I have a C# Web Service that is serializing my simple class:
[Serializable]
[XmlInclude(typeof(Bitmap))]
[XmlTypeAttribute(Namespace = "http://tempuri.org/")]
public class Class1
{
private static Bitmap _myImage = new Bitmap(#"C:\WebApplication1\ClassLibrary1\Untitled.png");
public Bitmap MyImage
{
get { return _myImage; }
set
{
_myImage = value;
}
}
}
Here's the asmx.cs code that does the serialization:
[WebMethod]
public string HelloWorld()
{
var c = new Class1();
XmlSerializer serializer = new XmlSerializer(typeof(Class1));
return XMLSerializer(c);
}
public string XMLSerializer(object pObject)
{
try
{
XmlSerializer xs = new XmlSerializer(pObject.GetType());
using (StringWriter stream = new StringWriter())
{
xs.Serialize(stream, pObject);
stream.Flush();
return stream.ToString();
}
}
catch (Exception ex)
{
return ex.ToString();
}
}
Looks prety straight forward. However, the XML generated by the XmlSerializer is producing and error when I try to DeSerialize it.
{"There is an error in XML document (5, 5)."}
{"Parameter is not valid."}
When I try to load the generated XML into IE I get this error.
Switch from current encoding to specified encoding not supported. Error processing resource 'file:///C:/Users/mhall15/Deskt...
<?xml version="1.0" encoding="utf-16"?>
Here's the generated XML:
<?xml version="1.0" encoding="utf-16"?>
<Class1 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<MyImage xmlns="http://tempuri.org/">
<Palette />
</MyImage>
</Class1>
Any ideas what's going on?
During the serialization, replace "encoding="utf-16" with "encoding="utf-8"" and that will cut it. The source of the problem - I'm not sure, but I've ran into it numerous times, and that's how I dealt with it.
That's how to deal with the IE issue.
The deserialization should be amongst these lines. I'm posting the kind of arbitrary code I normally use:
public static object DeserializeFromXML<T>(string _input)
{
object _temp = Activator.CreateInstance<T>();
Type expected_type = _temp.GetType();
_temp = null;
XmlSerializer serializer = new XmlSerializer(expected_type);
StringReader stringWriter = new StringReader(_input);
object _copy = serializer.Deserialize(stringWriter);
return _copy;
}
In the above example, I'm using templating for reusability sake. You should be able to call the method by saying DeserializeFromXML < Class1 >(_xml_input) where xml input is the string. That will force the compiler to use the definition of the given class to deserialize the XML input. That way you wouldn't even have to change the encoding in the serialization. If it's also a case where you may or may not know the data type to deserialize with, you can use a strategy design pattern where you register the root type of the XML with it's associated generic type. Later on you can reference that registry and arbitrarily deserialize any XML input as long as the root type is registered. It's a trick i normally use as well. If more is needed on this, let me know, and I'll explain in detail.
In addition,if you are running IE 9, the new update to IE 9 makes it difficult to view XML. Press F12 - go to developer tools and change your browser mode to run as IE 8 instead of IE 9.
I have seen a couple of examples on here where Xpath is used in conjunction with XmlDocument to get a specific attribute from an XmlDocument Node.... Example
Console.WriteLine(xmlDocument.SelectSingleNode("//dataTemplateSpecification/templates/template/elements/element/#name").Value.ToString());
For some reason I am getting a "Object reference not set to an instance of an object." exception. Whenever I run across that particular line of code. I have a little test app that I have set up to test out different things before I put them into my main project...
Here is the code for that...
namespace ReadXml
{
class Program
{
static void Main(string[] args)
{
//string fulXmlPath = System.Web.Hosting.HostingEnvironment.MapPath("~/App_Data/templateExample.xml");
XDocument xDocument = XDocument.Load("C:\\Users\\derekww\\Documents\\XML Documents\\templateExample.xml");
XElement elem = xDocument.Element("dataTemplateSpecification"); ;
XmlDocument xmlDocument = new XmlDocument();
StreamReader file = new StreamReader("C:\\Users\\derekww\\Documents\\XML Documents\\templateExample.xml");
xmlDocument.Load(file);
//XmlDocument theDoc = new XmlDocument();
//using (var xmlReader = xDocument.CreateReader())
//{
// xmlDocument.Load(xmlReader);
//}
//Console.WriteLine(elem.ToString());
XmlNode xNode = xmlDocument.SelectSingleNode("//dataTemplateSpecification/templates/template/elements/element");
Console.WriteLine("WORK PLEASE!!!! {0}", xNode.Value.ToString());
//Console.WriteLine(xmlDocument.SelectSingleNode("//dataTemplateSpecification/templates/template/elements/element/#name").Value.ToString());
//Console.WriteLine("This better Work>>>> {0}", xmlDocument.Attributes["/dataTemplateSpecification/templates/template/elements/element/#name"].Value);
Console.ReadLine();
//Console.WriteLine("This better Work>>>> {0}", xmlDocument.SelectSingleNode("//dataTemplateSpecification/templates/template/elements/element/#name").Value);
//foreach (String AttVal in xmlDocument.SelectSingleNode("//dataTemplateSpecification/templates/template/elements/element/#name").Value)
{
//Console.WriteLine("This better Work>>>> {0}", AttVal);
}
}
}
}
Here is part of the XML that I used...
<?xml version="1.0" encoding="utf-8"?>
<dataTemplateSpecification id="id1" name="name1" xmlns="http://EADIS.upmc.com /DataTemplateSpecification.xsd">
<description xmlns="">
<html>text</html>
</description>
<templates xmlns="">
<template>
<elements>
<element id="element0" name="PatientId" display="Patient ID" dataType="String" value="0101010111111" visable="false" readOnly="true">
<validation>
<rules>
<rule id="0" test="#element0.value == ''">
<fail>
<html><b>Patient ID is null, value must be present</b></html>
</fail>
</rule>
</rules>
</validation>
</element>
</elements>
</template>
<templates>
I just showed you the part that you need to understand the xml structure. I assure you that it is well formed. I think I asked this question before but somehow or the other it didn't get posted (maybe I forgot, who knows). Any help with this would be greatly appreciated. If I come up with a reason for why it isn't working I will be sure to let you guys know.
Thank You.
Why can't you use this XPath:
xmlDocument.SelectSingleNode("//templates/template/elements/element/#name").Value
You need to specify the namespace of the XML file in your code.
See here for more info: How to select xml root node when root node has attribute?
Okay this one DID it! Thanks to all of you!
public class Result
{
public String htmlEscaped
{
set;
get;
}
[XmlIgnore]
public String htmlValue
{ set; get; }
[XmlElement("htmlValue")]
public XmlCDataSection htmlValueCData
{
get
{
XmlDocument _dummyDoc = new XmlDocument();
return _dummyDoc.CreateCDataSection(htmlValue);
}
set { htmlValue = (value != null) ? value.Data : null; }
}
}
Result r = new Result();
r.htmlValue = ("<b>Hello</b>");
r.htmlEscaped = ("<b>Hello</b>");
XmlSerializer xml = new XmlSerializer(r.GetType());
TextWriter file = new StreamWriter(Environment.CurrentDirectory + "\\results\\result.xml", false, System.Text.Encoding.Default);
xml.Serialize(file, r);
file.Close();
RESULT:
<?xml version="1.0" encoding="Windows-1252"?>
<Result xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<htmlEscaped><b>Hello</b></htmlEscaped>
<htmlValue><![CDATA[<b>Hello</b>]]></htmlValue>
</Result>
As you can see, after CDATA is return type, no more escaped html in XML file on filesystem.
The JSON Serialization isn't working anymore, but this can be fixed with a little type extention.
QUESTION WAS:
Maybe someone knows how to make do it...
I have this Class:
public class Result
{
public String htmlValue
{
get;
set;
}
}
I use this to serialize it to XML
Result res = new Result();
res.htmlValue = "<p>Hello World</p>";
XmlSerializer s = new XmlSerializer(res.GetType());
TextWriter w = new StreamWriter(Environment.CurrentDirectory + "\\result.xml", false, System.Text.Encoding.Default);
s.Serialize(w, res);
w.Close();
Works fine i get this:
<?xml version="1.0" encoding="Windows-1252"?>
<Result xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<htmlValue><b>Hello World</b></htmlValue>
</Result>
What can do i have to change to get this:
<?xml version="1.0" encoding="Windows-1252"?>
<Result xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<htmlValue><![CDATA[<b>Hello World</b>]]></htmlValue>
</Result>
I've already searched but I can't find anything. The type of htmlValue
have to stay String, because of other Serialisations JSON, etc.
Tricky one... Thanks in advance for suggestions
HTML is correct in String within C#. Why decode or encode?
XmlSerializer saved the HTML escaped to XML file.
Don't use C# for consuming.
Is external tool which accept this:
<htmlValue><![CDATA[<b>Hello World</b>]]></htmlValue>
but not
<htmlValue><b>Hello World</b></htmlValue>
I do the same with JSON Serializer, in file on hard drive the HTML is saved correct.
Why and where to use HTTP Utility to prevent that? And how to get <![CDATA[ ]]> around it.
Can you give a code sample?
Are there any other Serializer than the C# own one?
I've found this Link .NET XML Serialization of CDATA ATTRIBUTE from Marco André Silva, which does I need to do, but it's different, how to include this without changing Types?
Here's a simple trick to do achieve what you want. You just need to serialize a XmlCDataSection property instead of the string property :
(it's almost the same as John's suggestion, but a bit simpler...)
public class Result
{
[XmlIgnore]
public String htmlValue
{
get;
set;
}
private static XmlDocument _dummyDoc;
[XmlElement("htmlValue")]
public XmlCDataSection htmlValueCData
{
get { return _dummyDoc.CreateCDataSection(htmlValue); }
set { htmlValue = (value != null) ? value.Data : null; }
}
}
See "CDATA serialization with XMLSerializer" for the same problem, and for the solution.
BTW, it seems to me that if the vendor no longer exists, it's time to use a different product. Possibly one that understands the XML specifications which have only existed for over a decade.
It is my understanding that you need the XML to feed it to some utility. Do you also plan to use it to de-serialize the object?
If not then why do not do it yourself - serialize your object that is? Roundtrip object -> XML -> object is somewhat tricky, but the first part is not.