I'm using HttpClient to post xml to a rest service. The problem is the service expects namespace prefix's in fashion that I'm unable to achieve with DataContractSerializer.
Expected xml:
<gto:createRequest xmlns:gto="http://www...com/sign">
<userId></userId>
<visibleDataContentType></visibleDataContentType>
<visibleData></visibleData>
<hiddenData></hiddenData>
<expiryInSeconds></expiryInSeconds>
</gto:createRequest>
The object i'm serialzing:
namespace ABC
{
[DataContract(Name = "createRequest", Namespace = "http://www...com/sign")]
public class CreateRequest
{
[DataMember(Name = "userId")]
public string UserId { get; set; }
[DataMember(Name = "visibleDataContentType")]
public string VisibleDataContentType { get; set; }
[DataMember(Name = "visibleData")]
public string VisibleData { get; set; }
[DataMember(Name = "hiddenData")]
public string HiddenData { get; set; }
[DataMember(Name = "expiryInSeconds")]
public int ExpiryInSeconds { get; set; }
}
}
I can't get the prefix "gto: createRequest", this what DataContractSerializer outputs:
<createRequest xmlns="http://www...com/sign" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<expiryInSeconds></expiryInSeconds>
<hiddenData></hiddenData>
<userId></userId>
<visibleData></visibleData>
<visibleDataContentType></visibleDataContentType>
</createRequest>
I have tried the old XmlSerializer but with no luck. Any ideas!?
Update: The namespace prefix does not have to be gto: but i has to be there!
Update: the output from Ondrej Svejdars answer that the server doesn't accept:
<gto:createRequest xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:gto="http://www.test.com/sign">
<gto:expiryInSeconds>60</gto:expiryInSeconds>
<gto:hiddenData>hidden</gto:hiddenData>
<gto:userId>123456</gto:userId>
<gto:visibleData>visible</gto:visibleData>
<gto:visibleDataContentType>text/plain</gto:visibleDataContentType>
</gto:createRequest>
[Edited to match the gto: only on top element]
You can tweak xml writer:
public class XmlProxyWritter : XmlTextWriter {
private string m_NS;
public XmlProxyWritter(string ns, TextWriter w)
: base(w) {
m_NS = ns;
}
public XmlProxyWritter(string ns, Stream w, Encoding encoding)
: base(w, encoding) {
m_NS = ns;
}
public XmlProxyWritter(string ns, string filename, Encoding encoding)
: base(filename, encoding) {
m_NS = ns;
}
public override string LookupPrefix(string ns) {
if (string.Compare(ns, m_NS, StringComparison.OrdinalIgnoreCase) == 0) {
return "gto";
}
return base.LookupPrefix(ns);
}
public override void WriteStartElement(string prefix, string localName, string ns) {
if (string.IsNullOrEmpty(prefix) && !string.IsNullOrEmpty(ns)) {
prefix = LookupPrefix(ns);
}
base.WriteStartElement(prefix, localName, ns);
}
}
Business class:
[XmlRoot(ElementName = "createRequest", Namespace = "http://www.test.com/sign")]
public class CreateRequest {
[XmlElement(ElementName="userId", Namespace = "")]
public string UserId { get; set; }
[XmlElement(ElementName = "visibleDataContentType", Namespace = "")]
public string VisibleDataContentType { get; set; }
[XmlElement(ElementName = "visibleData", Namespace = "")]
public string VisibleData { get; set; }
[XmlElement(ElementName = "hiddenData", Namespace = "")]
public string HiddenData { get; set; }
[XmlElement(ElementName = "expiryInSeconds", Namespace = "")]
public int ExpiryInSeconds { get; set; }
}
Call example (where http://www.test.com/sign is the namespace of CreateRequest)
string result;
var serXml = new XmlSerializer(typeof(CreateRequest));
using (var stream = new MemoryStream()) {
using (var writer = new XmlProxyWritter("http://www.test.com/sign", stream, Encoding.UTF8)) {
serXml.Serialize(writer, new CreateRequest {
ExpiryInSeconds = 1,
HiddenData = "my preasures",
UserId = "Pepa"
});
}
result = Encoding.UTF8.GetString(stream.ToArray());
}
Output:
<?xml version="1.0" encoding="utf-8"?>
<gto:createRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:gto="http://www.test.com/sign">
<userId>Pepa</userId>
<hiddenData>my preasures</hiddenData>
<expiryInSeconds>1</expiryInSeconds>
</gto:createRequest>
Which works for you (I hope) but it feels like kind of hacking; maybe the proper solution here would be to "teach" server the correct xml format ? :)
Related
I'm deserialization the results of a request that has the same tag repeated at multiple levels, I have it working but to do so I'm changing the format of the XML before attempting to deserialize it.
I'm not able to edit the source of the XML to change it to only have Diary at one level.
Is there a way to adjust my XML attributes to handle the deserialization without needing to adjust the response?
XML Response
<?xml version="1.0" ?>
<Root>
<DiaryDetails>
<Diary>
<Diary created_user="value1" created_date="value2" long_text="value3" short_text="value4" entry_type="value5" >Value6</Diary>
</Diary>
<Diary>
<Diary created_user="value7" created_date="value8" long_text="value9" short_text="value10" entry_type="value11" >Value12</Diary>
</Diary>
</DiaryDetails>
</Root>
Class definition
[XmlRoot("DiaryDetails")]
public class Diaries : List<Diary> { }
[XmlRoot("Diary")]
public class Diary
{
[XmlAttribute("created_user")]
public string CreatedBy { get; set; }
[XmlAttribute("created_date")]
public string CreatedDate { get; set; }
[XmlAttribute("long_text")]
public string LongText { get; set; }
[XmlAttribute("short_text")]
public string ShortText { get; set; }
[XmlAttribute("entry_type")]
public string Type { get; set; }
}
Deserialization Method
internal T DeserilaiseObject<T>(string response)
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
T DeserilaisedObject;
using (TextReader reader = new StringReader(response))
{
DeserilaisedObject = (T)serializer.Deserialize(reader);
}
return DeserilaisedObject;
}
I'm currently handling this with a string replace:
response = response.Replace("<Diary><Diary", "<Diary").Replace("</Diary></Diary>", "</Diary>");
Try following :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApplication40
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XmlReader reader = XmlReader.Create(FILENAME);
XmlSerializer serializer = new XmlSerializer(typeof(Root));
Root root = (Root)serializer.Deserialize(reader);
}
}
[XmlRoot("Root")]
public class Root
{
[XmlArray("DiaryDetails")]
[XmlArrayItem("Diary")]
public List<DiaryMain> diaries { get; set; }
}
public class DiaryMain
{
public Diary Diary { get; set; }
}
[XmlRoot("Diary")]
public class Diary
{
[XmlAttribute("created_user")]
public string CreatedBy { get; set; }
[XmlAttribute("created_date")]
public string CreatedDate { get; set; }
[XmlAttribute("long_text")]
public string LongText { get; set; }
[XmlAttribute("short_text")]
public string ShortText { get; set; }
[XmlAttribute("entry_type")]
public string Type { get; set; }
}
}
Assuming that you are only interested at the attributed Diary elements at the second nesting level you could do this:
// load your xml into a document
var doc = new XmlDocument();
doc.Load("your_xml_file.xml");
// extract the interesting nodes using xpath
var nodes = doc.SelectNodes("//Diary/Diary");
// deserialize the filtered NodeList (yes it's that clunky)
var serializer = new XmlSerializer(typeof(Diary));
var diaries = nodes.Cast<XmlNode>().Select(x => serializer.Deserialize(new XmlNodeReader(x))).Cast<Diary>().ToArray();
I need to serialize a class to xml. If a certain condition is met at run-time, I want to add an XML attribute to an element and assign it a value. Sometimes, the "Error" attribute will appear and sometimes it won't.
My code that serializes my objects:
public class XmlToolsRepo : IXmlTools
{
public string SerializeToXML<T>(object obj)
{
string results = null;
Encoding enc = Encoding.UTF8;
using (MemoryStream ms = new MemoryStream())
{
using (XmlTextWriter xw = new XmlTextWriter(ms, enc))
{
xw.Formatting = Formatting.None;
XmlSerializerNamespaces emptyNS = new XmlSerializerNamespaces(new[] { new XmlQualifiedName("", "") });
XmlSerializer xSerializer = new XmlSerializer(typeof(T));
xSerializer.Serialize(xw, obj, emptyNS);
}
results = enc.GetString(ms.ToArray());
}
return results;
}
}
A class with a property that could have a new attribute at run-time:
[DataContract]
public class H204
{
[DataMember]
[XmlAttribute]
public string Code { get; set; }
[DataMember]
public string DW { get; set; }
}
When a condition is met I need for the XML to look like this:
<?xml version="1.0" encoding="UTF-8"?>
<H204 Code="A">
<DW Error="test" />
</H204>
Try following :
public class H204
{
[XmlAttribute(AttributeName = "Code")]
public string Code { get; set; }
[XmlElement(ElementName = "DW")]
public DW dw{ get; set; }
}
public class DW
{
[XmlAttribute(AttributeName = "Error")]
public string text { get; set; }
}
I'm trying to obtain the value of the logicalName attribute of the main node of this xml file:
<?xml version="1.0" encoding="ISO-8859-1"?>
<ticketlayout xmlns="http://www.example.com/ticketlayout" logicalName="target.xml" deviceCode="1" measurement="mm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.com/ticketlayout">
<fontdefinition id="BarCode">
<fontname>Code128bWin</fontname>
<size measure="pt">16</size>
</fontdefinition>
</ticketlayout>
I've tried to add the namespace "xsi", "http://www.w3.org/2001/XMLSchema-instance" this way:
XmlDocument fLayout = new XmlDocument();
fLayout.Load("myFile.xml");
XmlNamespaceManager nsmRequestLayout = new XmlNamespaceManager(fLayout.NameTable);
nsmRequestLayout.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
string sValue = fLayout.SelectNodes("//ticketlayout", nsmRequestLayout)[0].Attributes["name"].Value;
But I get no nodes. I've tried without namespace and no nodes again, and son on.
¿Could please anyone help me?
Thanks in advance.
If you want to get the value : target.xml
Try this code
XmlDocument fLayout = new XmlDocument();
fLayout.Load("myFile.xml"); // your XML file
var attrib = fLayout["ticketlayout"].Attributes["logicalName"].Value;
First of all your XML is not valid.
I modified to look this way in order to achieve what you are looking for.
XML File :
<?xml version="1.0" encoding="UTF-8"?>
<ticketlayout xmlns="http://www.example.com/ticketlayout" logicalName="target.xml" deviceCode="1" measurement="mm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.com/ticketlayout">
<fontdefinition id="BarCode">
<fontname>Code128bWin</fontname>
<size measure="pt">16</size>
</fontdefinition>
</ticketlayout>
I am not sure why you would not have a model structure to deserialize you xml, and then access whatever property/attribute you need.
Example :
Classes:
[XmlRoot(ElementName = "size", Namespace = "http://www.example.com/ticketlayout")]
public class Size
{
[XmlAttribute(AttributeName = "measure")]
public string Measure { get; set; }
[XmlText]
public string Text { get; set; }
}
[XmlRoot(ElementName = "fontdefinition", Namespace = "http://www.example.com/ticketlayout")]
public class Fontdefinition
{
[XmlElement(ElementName = "fontname", Namespace = "http://www.example.com/ticketlayout")]
public string Fontname { get; set; }
[XmlElement(ElementName = "size", Namespace = "http://www.example.com/ticketlayout")]
public Size Size { get; set; }
[XmlAttribute(AttributeName = "id")]
public string Id { get; set; }
}
[XmlRoot(ElementName = "ticketlayout", Namespace = "http://www.example.com/ticketlayout")]
public class Ticketlayout
{
[XmlElement(ElementName = "fontdefinition", Namespace = "http://www.example.com/ticketlayout")]
public Fontdefinition Fontdefinition { get; set; }
[XmlAttribute(AttributeName = "xmlns")]
public string Xmlns { get; set; }
[XmlAttribute(AttributeName = "logicalName")]
public string LogicalName { get; set; }
[XmlAttribute(AttributeName = "deviceCode")]
public string DeviceCode { get; set; }
[XmlAttribute(AttributeName = "measurement")]
public string Measurement { get; set; }
[XmlAttribute(AttributeName = "xsi", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Xsi { get; set; }
[XmlAttribute(AttributeName = "schemaLocation", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
public string SchemaLocation { get; set; }
}
Then you could use a serializer :
public class Serializer
{
public T Deserialize<T>(string input) where T : class
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
using (StringReader stringReader = new StringReader(input))
{
return (T)xmlSerializer.Deserialize(stringReader);
}
}
public string Serialize<T>(T ObjectToSerialize)
{
XmlSerializer xmlSerializer = new XmlSerializer(ObjectToSerialize.GetType());
StringBuilder builder = new StringBuilder();
using (StringWriterWithEncoding textWriter = new StringWriterWithEncoding(builder, Encoding.UTF8))
{
xmlSerializer.Serialize(textWriter, ObjectToSerialize);
return textWriter.ToString();
}
}
}
public class StringWriterWithEncoding : StringWriter
{
Encoding encoding;
public StringWriterWithEncoding(StringBuilder builder, Encoding encoding)
: base(builder)
{
this.encoding = encoding;
}
public override Encoding Encoding
{
get { return encoding; }
}
}
And finally you can access whatever you want by doing the following :
var serializer = new Serializer();
//I used a local file for testing, but it should be the same thing with your file
var xmlInputData = File.ReadAllText(#"MyXmlPath");
var output = serializer.Deserialize<Ticketlayout >(xmlInputData);
var logicalName = output.LogicalName;
I am having some issue with deserialising some XML to an object list, always getting count 0 and within that "raw data" when inspecting DateAndTimeSlot during debug.
Unfortunately I cannot change the names of these elements.
However when checking the XML I get back, there are DateAndTimeslot objects in the XML.
With other object lists I have all seems fine, without the inclusion of namespaces.
What have I missed?
C# Code:
[XmlRoot("AppointmentAvailabilityStatusResponse")]
public class CheckAppointmentAvailabilityContainer
{
[XmlElement("AppointmentAvailabilityStatusResult")]
public AppointmentAvailabilityStatus appointmentAvailabilityStatus { get; set; }
}
[XmlRoot("AppointmentAvailabilityStatusResult", Namespace = "Appointments")]
public class AppointmentAvailabilityStatus
{
[XmlArray("DateAndTimeSlot", Namespace = "Appointments")]
[XmlArrayItem(typeof(DateAndTimeslot))]
public DateAndTimeSlots DateAndTimeSlot { get; set; }
[XmlElement("RequestedStatus")]
public int RequestedStatus { get; set; }
}
[XmlRoot(ElementName = "DateAndTimeSlot")]
[XmlType("a")]
public class DateAndTimeSlots : List<DateAndTimeslot> { }
[XmlRoot(ElementName = "DateAndTimeslot", Namespace = "Appointments.TO")]
[XmlType("b")] // if included this renames the node to "b" for some reason
public class DateAndTimeslot
{
[XmlElement("Date")]
public string Date { get; set; }
[XmlElement("TimeSlot")]
public string TimeSlot { get; set; }
}
Shortened XML returned that I wish to fully deserialise.
<AppointmentAvailabilityStatusResponse>
<AppointmentAvailabilityStatusResult xmlns:a="Appointments" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<a:DateAndTimeSlot xmlns:b="Appointments.TO">
<b:DateAndTimeslot>
<b:Date>14/07/2016</b:Date>
<b:TimeSlot>AM</b:TimeSlot>
</b:DateAndTimeslot>
<b:DateAndTimeslot>
<b:Date>14/07/2016</b:Date>
<b:TimeSlot>PM</b:TimeSlot>
</b:DateAndTimeslot>
</a:DateAndTimeSlot>
<a:RequestStatus>0</a:RequestStatus>
</AppointmentAvailabilityStatusResult>
</AppointmentAvailabilityStatusResponse>
XML if I serialise a dummy object - some differences which I'm trying to rectify, not sure if the namespaces are necessary for deserialisation though
<AppointmentAvailabilityStatusResponse>
<AppointmentAvailabilityStatusResult>
<DateAndTimeSlot xmlns=\"Appointments\">
<DateAndTimeslot>
<Date xmlns=\"Appointments.TO\">today</Date>
<TimeSlot xmlns=\"Appointments.TO\">now</TimeSlot>
</DateAndTimeslot>
</DateAndTimeSlot>
<RequestedStatus xmlns=\"Appointments\">0</RequestedStatus>
</AppointmentAvailabilityStatusResult>
</AppointmentAvailabilityStatusResponse>
Deserialiser
public static T DeserializeThis<T>(string cleanXml)
{
//string cleanXml = RemoveBom(dirtyXml);
bool check = cleanXml.TrimStart().StartsWith("<");
if (!string.IsNullOrEmpty(cleanXml) && cleanXml.TrimStart().StartsWith("<"))
{
try
{
XmlSerializer xs = new XmlSerializer(typeof(T));
MatchCollection mc = Regex.Matches(cleanXml, #"</?(\d\w+)");
List<string> elements = new List<string>();
foreach (Match m in mc)
{
string cpval = m.Groups[1].Value;
if (!elements.Contains(cpval)) { elements.Add(cpval); }
}
foreach (string e in elements)
{
cleanXml = cleanXml.Replace(e, "d_" + e);
}
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(cleanXml)))
{
using (StringReader sr = new StringReader(cleanXml))
{
return (T)xs.Deserialize(sr);
}
}
}
catch(XmlException x)
{
var obj = (T)Activator.CreateInstance(typeof(T));
Type type = obj.GetType();
return (T)obj;
}
}
else
{
var obj = (T)Activator.CreateInstance(typeof(T));
Type type = obj.GetType();
// add in the generic derived class property search and assign
return (T)obj;
}
}
Thank you to those who commented above - I finally got it working. Removing the XmlArray and not including Anonymous and IsNullable attributes seemed to be the issue although I am unsure why as it works with all of the other functions I have, serializable possibly doesn't need to be present either.
Working class structure minus the container as that didn't change:
[Serializable()]
[XmlType(AnonymousType = true, Namespace = "")]
public class AppointmentAvailabilityStatusResult : WebserviceMessage
{
[XmlElement("DateAndTimeSlot", Namespace = "Appointments")]
public DateAndTimeSlot DateAndTimeSlot { get; set; }
[XmlElement("RequestedStatus")]
public int RequestedStatus { get; set; }
}
[Serializable()]
[XmlType(AnonymousType = true, Namespace = "Appointments")]
[XmlRoot(ElementName = "DateAndTimeSlot",Namespace = "Appointments", IsNullable = false)]
public class DateAndTimeSlot
{
[XmlElement(ElementName = "DateAndTimeslot", Namespace = "Appointments.TO")]
public List<DateAndTimeslot> DateAndTimeslot { get; set; }
}
[Serializable()]
[XmlType(AnonymousType = true, Namespace = "Appointments.TO")]
[XmlRoot(Namespace = "Appointments.TO", IsNullable = false)]
public class DateAndTimeslot
{
[XmlElement("Date")]
public string Date { get; set; }
[XmlElement("TimeSlot")]
public string TimeSlot { get; set; }
}
I have this class which represent a node TestCase in my XML :
public class TestCase
{
[XmlAttribute("name")]
public string name { get; set; }
public string version { get; set; }
public string verdict { get; set; }
public string objective { get; set; }
public string criteria { get; set; }
public string issue { get; set; }
public string clientcomments { get; set; }
public string authoritycomments { get; set; }
public string sdk { get; set; }
}
I use XmlNode.SelectSingleNode to fetch a specific node in my XML. For info, there are no duplicate nodes (no nodes with the same name attribute) if it matters.
So far, I have this code :
public static TestCase FetchNode(string NodeName, string Path)
{
TestCase testcase = new TestCase();
string[] attr = { "name", "version", "verdict", "objective", "criteria"
, "issue", "clientcomments", "authoritycomments", "sdk" };
string[] attrval = { null, null,null,null,null,null,null,null,null};
XmlDocument doc = new XmlDocument();
doc.Load(Path);
XmlNode node = doc.SelectSingleNode("/TestsList/TestCase[#name='" + NodeName + "']");
for (var i = 0; i == attr.Length - 1;i++)
{
attrval[i] = node[attr[i]].InnerText;
}
testcase.name = attrval[0];
testcase.version = attrval[1];
testcase.verdict = attrval[2];
testcase.objective = attrval[3];
testcase.criteria = attrval[4];
testcase.issue = attrval[5];
testcase.clientcomments = attrval[6];
testcase.authoritycomments = attrval[7];
testcase.sdk = attrval[8];
return testcase;
}
However, this code is not scalable at all, if I change my class structure, I would need to change the function because each element of the class are hardcoded in it.
This is a wide request, but how could I write this function so if I add or remove a string in the class definition of TestCase, I don`t have to change the function FetchNode.
Thank you for your time.
You could use XmlSerializer.Deserialize
Example:
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
public class TestCase
{
[XmlAttribute("name")]
public string name { get; set; }
public string version { get; set; }
public string verdict { get; set; }
public string objective { get; set; }
public string criteria { get; set; }
public string issue { get; set; }
public string clientcomments { get; set; }
public string authoritycomments { get; set; }
public string sdk { get; set; }
}
public class Program
{
public const string XML = #"
<TestCase name='TicketName'>
<name>Jon Nameson</name>
<version>10.1</version>
<verdict>High</verdict>
</TestCase>
";
public static void Main()
{
var doc = new XmlDocument();
doc.LoadXml(XML);
var node = doc.SelectSingleNode("/TestCase");
var serializer = new XmlSerializer(typeof(TestCase));
var testcase = serializer.Deserialize(new StringReader(node.OuterXml)) as TestCase;
Console.WriteLine(testcase.name);
Console.WriteLine(testcase.version);
Console.WriteLine(testcase.verdict);
}
}
DotNetFiddle
You can deserialize directly from your selected XmlNode by combining XmlSerializer with XmlNodeReader using the following extension method:
public static class XmlNodeExtensions
{
public static T Deserialize<T>(this XmlNode element, XmlSerializer serializer = null)
{
using (var reader = new ProperXmlNodeReader(element))
return (T)(serializer ?? new XmlSerializer(typeof(T))).Deserialize(reader);
}
class ProperXmlNodeReader : XmlNodeReader
{
// Bug fix from https://stackoverflow.com/questions/30102275/deserialize-object-property-with-stringreader-vs-xmlnodereader
public ProperXmlNodeReader(XmlNode node)
: base(node)
{
}
public override string LookupNamespace(string prefix)
{
return NameTable.Add(base.LookupNamespace(prefix));
}
}
}
This adds an extension method to XmlNode which invokes XmlSerializer to deserialize the selected node to an instance of the generic type T.
Then do:
var testcase = node.Deserialize<TestCase>();
which is identical to:
var testcase = XmlNodeExtensions.Deserialize<TestCase>(node);
In your case the expected root element name of your TestCase class, namely <TestCase>, matches the actual node name. If the node name does not match the expected root element name, you can tell XmlSerializer to expect a different root name by following the instructions in XmlSerializer Performance Issue when Specifying XmlRootAttribute.