XML deserialization with or without namespace - c#

I am trying to serialize and deserialize responses I am getting from service. The response is xml. The problem is that response can contain namespaces.
This is a response example which contains xmlns in root element and xmlns:ns2 in child:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<RootElement version="4.0" xmlns="http://www.test.com/test">
<Verification xmlns="" xmlns:ns2="http://www.test.com/test">
<!--some fields-->
</Verification>
</RootElement>
This is another response which does not contain xmlns and xmlns:n2:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<RootElement version="2.0">
<Verification>
<!--some fields-->
</Verification>
</RootElement>
To deserialize this response and then to serialize again I am using this code:
public static object XmlDeserializeFromString(this string objectData, Type type)
{
var serializer = new XmlSerializer(type);
object result;
using (TextReader textReader = new StringReader(objectData))
{
using (XmlTextReader xmlReader = new XmlTextReader(textReader))
{
// xmlReader.Namespaces = false;
result = serializer.Deserialize(xmlReader);
}
}
return result;
}
public static string XmlSerializeToString(this object objectInstance, System.Text.Encoding encoding)
{
var serializer = new XmlSerializer(objectInstance.GetType());
var sb = new StringBuilder();
var settings = new XmlWriterSettings
{
Encoding = encoding ?? System.Text.Encoding.UTF8,
Indent = true,
NewLineHandling = NewLineHandling.Replace
};
using (var sw = new EncodedStringWriter(encoding, sb))
{
using (var xw = XmlWriter.Create(sw, settings))
{
serializer.Serialize(xw, objectInstance);
}
}
return sb.ToString();
}
The class: This works for first type or responses but not for second. If I'll remove Namespace from attribute it will work for second but not for first type.
[Serializable, XmlRoot("RootElement", IsNullable = false, Namespace="http://www.test.com/test")]
public class RootElement
{
[XmlAttribute("version")] public string Version;
[XmlElement(Namespace="http://www.test.com/test")]
public Verification[] Verification;
}
public class Verification
{
[XmlAttribute("vendor")] public string Vendor;
}
with xmlReader.Namespaces = false line uncommented deserialization fails for first example, with commented it fails for second example. I tried to override the NamespaceURL property from XmlTextReader to ignore namespaces like it is described here and in other questions and answers and articles, also tried to override Read() method but it does not helped. I also tried to add or remove urls from class attributes but it does not helped too.
Is it possible to create a serialize and deserialize methods which can do the job for both responses? What I am doing wrong?

Related

Deserializing XML Array in unity

Im trying to deserialize an array of objects from a XML Document.
The document built in the following structure:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<element>
.....
</element>
<element>
.....
</element>
</root>
But for some reason Im having lots of problems doing so.
This is my function which I call to deserialize it:
public static CardModel[] Load(string text)
{
XmlRootAttribute xRoot = new XmlRootAttribute();
xRoot.ElementName = "root";
xRoot.IsNullable = true;
XmlSerializer serializer = new XmlSerializer(typeof(CardModel[]),xRoot);
StringReader reader = new StringReader(text);
CardModel[] o = serializer.Deserialize(reader) as CardModel[];
reader.Close();
return o;
}
And I am not sure if its done correctly or not. Because I know that in json you are unable to deserialize an array and you have to do some sort of "hack".
In the CardModel class (which is the element of the array) i use above the class the tag [XmlRoot("root")]. I have also tried to use [XmlRoot("element")] but still im getting stuck.
Afaik you can't directly deserialize into an array but would need a wrapper class like
[Serializable]
[XMLRoot("root")]
public class Root
{
// This does the magic of treating all "element" items nested under the root
// As part of this array
[XmlArray("element")]
public CardModel[] models;
}
And rather deserilialize into that like
public static CardModel[] Load(string text)
{
// I don't think that you need the attribute overwrite here
var serializer = new XmlSerializer(typeof(Root));
using(var reader = new StringReader(text))
{
var root = (Root) serializer.Deserialize(reader);
return root.models;
}
}

XML error during deserialize

I have problem when I'm trying to deserialize an XML to object. My XML look like:
<?xml version="1.0" encoding="utf-16"?>
<Products
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<AllProducts>
<Product>
<ID>8</ID>
<GID>51280</GID>
<Kod>RNIKAKC1.6</Kod>
<Name>SB-800</Name>
<Ean>0018208048014</Ean>
<CommodityGroup>
<ID>86</ID>
<Name>AKCESORIA FOTO</Name>
<Path>
<Category>
<ID>60798</ID>
<Name>ARCHIWALNE</Name>
</Category>
</Path>
</CommodityGroup>
</Product>
....
Next products
...
My method code:
var MemoryStream = APIAccess.DownloadFileToStream("example.xml", "exampleContainer");
using (MemoryStream)
{
MemoryStream.Position = 0;
using (StreamReader StreamReader = new StreamReader(MemoryStream))
{
XmlSerializer serializer = new XmlSerializer(typeof(CommodityGroup));
var products = serializer.Deserialize(StreamReader);
}
}
Method DownloadFileToStream is working good, because it is useful in other classes.
I'm geting error:
InvalidOperationException: Products xmlns='' was not expected.
I want to create object of a Node CommodityGroup. I've created class selecting this node, coping it and pasting in the new class like Paste Special -> XML
Attributes of this class looks like:
[Serializable()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[XmlTypeAttribute(AnonymousType = true)]
[XmlRootAttribute(Namespace = "CommodityGroup", IsNullable = false)]
I don't know to fix it. When I'm adding into XML Serializer param new XmlRootAttribute("Products"), I'm getting "0" values.
Do you have any suggestions?
If you want to deserialize only part of an xml document, you should skip unnecessary nodes.
Do it using XmlReader.
using (StreamReader StreamReader = new StreamReader(MemoryStream))
using (var xmlReader = XmlReader.Create(StreamReader))
{
xmlReader.ReadToFollowing("CommodityGroup");
XmlSerializer serializer = new XmlSerializer(typeof(CommodityGroup));
var commodityGroup = (CommodityGroup)serializer.Deserialize(xmlReader);
}

Trouble deserialising xml file using DataContractSerializer

It's been awhile since i've used DataContractSerializer and i'm having a little trouble deserializing a xml file.
<?xml version="1.0" encoding="utf-8"?>
<SoftwareLicense xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
The error i'm getting is:
{"Error in line 1 position 117. Expecting element 'SoftwareLicense' from namespace 'http://schemas.datacontract.org/2004/07/Solentim.Modules.Licensing.Activation'..
Encountered 'Element' with name 'SoftwareLicense', namespace ''. "}
[DataContract(Name = "SoftwareLicense")]
public class SoftwareLicense : ISoftwareLicense
{
...
}
I've tried specifying the namespace which also doesn't work.
var serializer = new DataContractSerializer(typeof(SoftwareLicense));
using (var stream = new FileStream(filename, FileMode.Open, FileAccess.Read))
{
using (var reader =
XmlDictionaryReader.CreateTextReader(stream, new XmlDictionaryReaderQuotas()))
{
return (SoftwareLicense) serializer.ReadObject(reader);
}
}
The namespace of the file has recently changed and an interface added to the class. I've resolved all other issues i just can't seem to get around this one.
I prefer to use the DatacontractSerializer as the class now has interface properties and XMLSerializer won't deserialise it
This answer may help you to solve your problem.
If you prefer to use XmlSerializer. Here is the simple implementation below:
private T Deserialize<T>(string path) where T : class
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
T result = null;
using (XmlReader reader = XmlReader.Create(path))
{
result = (T)serializer.Deserialize(reader);
}
return result;
}

Putting c# inside xml

I am using WinForms. I have an XML document that looks like this:
<?xml version="1.0" encoding="utf-8" ?>
<MarcusXMLFile xmlns:Responses="http://www.rewardstrike.com/XMLFile1.xml">
<response>
<greatmood>
<yes>
<replytocommand>
<answer>Yes.</answer>
<answer>Yes, sir.</answer>
<answer>Settings.Default.User</answer>
</replytocommand>
</yes>
</greatmood>
</response>
</MarcusXMLFile>
To read this xml document, I use:
private void Responses()
{
string query = String.Format("http://www.rewardstrike.com/XMLFile1.xml");
XmlDocument Responses = new XmlDocument();
Responses.Load(query);
XmlNode channel = Responses.SelectSingleNode("MarcusXMLFile");
if (QEvent == "yesreplytocommand")
{
XmlNodeList yesreplytocommand = Responses.SelectNodes("MarcusXMLFile/response/greatmood/yes/replytocommand/answer");
foreach (XmlNode ans in yesreplytocommand
.Cast<XmlNode>()
.OrderBy(elem => Guid.NewGuid()))
{
response = ans.InnerText;
}
}
}
and then to display:
QEvent = "yesreplytocommand";
Responses();
Console.WriteLine(response);
My problem is when it gets Settings.Default.User and displays it, I want it to display it as c# code so that it actually gets the value from the application. Right now it is actually displaying "Settings.Default.User". How do I do this?
First, you'll need a way to recognize which of your entries are literals and which are expressions. You could do it by adding an attribute to the XML node:
<?xml version="1.0" encoding="utf-8" ?>
<MarcusXMLFile xmlns:Responses="http://www.rewardstrike.com/XMLFile1.xml">
<response>
<greatmood>
<yes>
<replytocommand>
<answer>Yes.</answer>
<answer>Yes, sir.</answer>
<answer expression="true">DefaultSettings.User</answer>
</replytocommand>
</yes>
</greatmood>
</response>
</MarcusXMLFile>
Based on that you can modify your parsing code to either directly use the value from XML or evaluate it instead:
foreach (XmlNode ans in yesreplytocommand
.Cast<XmlNode>()
.OrderBy(elem => Guid.NewGuid()))
{
var attribute = ans.Attributes["expression"];
if (attribute != null && attribute.Value == "true")
{
Console.WriteLine(Evaluate(ans.InnerText));
}
else
{
Console.WriteLine(ans.InnerText);
}
}
There's still the problem of evaluating that expression. There's no easy built-in way to do that from C#. But you could use Dynamic Expresso. This is how Evaluate method could look like:
public string Evaluate(string expression)
{
var interpreter = new Interpreter();
interpreter.SetVariable("DefaultSettings", Settings.Default);
return interpreter.Eval<string>(expression);
}
As you can see, you'll still have to define the expression variables yourself. For the above to work, you will have to use DefaultSettings.User in your XML instead of Settings.Default.User. I already made that change in my sample XML at the beginning of the answer.
You should take a look at XML Serialization.
Really basic on how it works is that it can convert a struct or a class like this:
struct Foo
{
int bar = 0;
Vector2 obj = new Vector2(10, 50);
}
into this:
<?xml version="1.0" ?>
<Foo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"">
<bar>0</bar>
<obj>
<X>10</X>
<Y>50</Y>
</ojb>
</Foo>
And the other way around.
The methods used to load and save code looks like this:
public static void Save(string filepath, Foo foobject)
{
XmlSerializer serializer = new XmlSerializer(typeof(Foo));
using (Stream stream = File.OpenWrite(filepath))
{
serializer.Serialize(stream, foobject);
}
}
public static Foo Load(string filepath)
{
Foo myFoo;
XmlSerializer serializer = new XmlSerializer(typeof(Foo));
using (Stream stream = File.OpenRead(filepath))
{
myFoo = (Foo)serializer.Deserialize(stream);
}
}
It converts xml code to c# code, and other way around.
It cannot convert methods, but it can convert most properties and classes.

Deserializing WCF Message at Server

I implemented a custom message inspector (via IDispatchMessageInspector) to intercept messages that are received on the server side of a WCF service so I can attempt to deserialize the message and apply some specific business logic. The problem I'm encountering is when I write the MessageBuffer's contents to a new MemoryStream and then try to deserialize, I get an error that says "The data at the root level is invalid. Line 1, position 1." I do know the data being passed in is valid as skipping over the inspector makes everything work fine.
Sample Code:
public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext)
{
MessageBuffer buffer = request.CreateBufferedCopy(Int32.MaxValue);
request = buffer.CreateMessage();
string msg = buffer.CreateMessage().ToString();
var dc = new DataContractSerializer(typeof(Adder));
using (var stream = new MemoryStream())
{
buffer.WriteMessage(stream);
stream.Position = 0;
//deserializing error occurs here
var c = dc.ReadObject(stream);
}
return null;
}
Here is the Adder class/interface:
[DataContract(Name = "adder", Namespace = "http://test.com")]
public class Adder
{
[DataMember(Name = "first")]
public int First { get; set; }
[DataMember(Name = "second")]
public int Second { get; set; }
}
[ServiceContract(Namespace = "http://test.com")]
public interface ITestSvc
{
[OperationContract(Name = "add")]
int Add(Adder adder);
}
Any suggestions or is there a better option for this? My main goal is to read the XML (in a deserialized object) on every WCF request that comes into my service.
The request object contains the WCF message headers as well as the payload. You'll need to strip off the headers and then you should be able to deserialize the message body.
For example, a SOAP message would have:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Header>
</soap:Header>
<soap:Body>
<!-- your payload -->
</soap:Body>
You could use XML navigation to get to the body element and then you'd deserialize that element alone.
EDIT:
Actually just stumbled on this method which I think should do the trick for you:
Message.GetReaderAtBodyContents
I just did this. Just paste all of the following code into your class, and call DeserializedResponse(). You'll need to change MyResponseObject to the name of whatever object you are trying to deserialize. Also, you will need to replace "_requestInspector.Response" with your own response xml string variable. The method GetSoapBodyInnerXml() will strip off the Soap Envelope, and only return your response xml which you wish to deserialize.
private MyResponseObject DeserializedResponse()
{
var rootAttribute = new XmlRootAttribute("MyResponseObject ");
rootAttribute.Namespace = #"http://www.company.com/MyResponseObjectNamespace";
XmlSerializer serializer = new XmlSerializer(typeof(MyResponseObject ), rootAttribute);
string responseSoapBodyInnerXml = GetSoapBodyInnerXml(_requestInspector.Response);
AddXmlDeclaration(ref responseSoapBodyInnerXml);
MemoryStream memStream = new MemoryStream(Encoding.UTF8.GetBytes(responseSoapBodyInnerXml));
MyResponseObject resultingResponse = (MyResponseObject )serializer.Deserialize(memStream);
return resultingResponse;
}
private string GetSoapBodyInnerXml(string soapMessage)
{
XDocument xDoc = XDocument.Parse(soapMessage);
XNamespace nsSoap = #"http://schemas.xmlsoap.org/soap/envelope/";
return xDoc.Descendants(nsSoap + CONST_SoapBody).Descendants().First().ToString();
}
private void AddXmlDeclaration(ref string xmlString)
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlString);
//Create an XML declaration.
XmlDeclaration xmldecl;
xmldecl = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
//Add the new node to the document.
XmlElement root = doc.DocumentElement;
doc.InsertBefore(xmldecl, root);
//Return updated xmlString with XML Declaration
xmlString = doc.InnerXml;
}
I ended up going the message header route, like Mike Parkhill suggested.

Categories