I am having a very hard time trying to deserialize the soap response below.
I assume its because of the multiple namespaces or maybe because of the complex type (Serialization Array)
Soapformatter throws an Object reference exception and other more manual deserializations are returning an empty object. At this point I can only assume that I am not tagging my object correctly. What is the correct way to build out the BatchResponse object below so that it can deserialize from this response?
<?xml version="1.0" encoding="UTF-8"?>
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">
<s:Header>
<a:Action s:mustUnderstand="1">https://example.com/operations/fetch/BatchResponse</a:Action>
</s:Header>
<s:Body>
<BatchResponse xmlns="https://example.com/operations">
<BatchResult xmlns:b="https://example.com/responses" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<b:FailureMessages xmlns:c="http://schemas.microsoft.com/2003/10/Serialization/Arrays" i:nil="true" />
<b:FailureType i:nil="true" />
<b:ProcessedSuccessfully>true</b:ProcessedSuccessfully>
<b:SessionId>1961810</b:SessionId>
<b:TotalPages>38</b:TotalPages>
</BatchResult>
</BatchResponse>
</s:Body>
</s:Envelope>
Also assuming Soapformatter continues to throw exceptions - what is the correct way to "xpath" to the BatchResponse object so that I can extract and deserialize?
Thanks
Try following :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApplication131
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XmlReader reader = XmlReader.Create(FILENAME);
XmlSerializer serializer = new XmlSerializer(typeof(Envelope));
Envelope envelope = (Envelope)serializer.Deserialize(reader);
}
}
[XmlRoot(ElementName = "Envelope", Namespace = "http://www.w3.org/2003/05/soap-envelope")]
public class Envelope
{
[XmlElement(ElementName = "Header", Namespace = "http://www.w3.org/2003/05/soap-envelope")]
public Header header { get; set; }
[XmlElement(ElementName = "Body", Namespace = "http://www.w3.org/2003/05/soap-envelope")]
public Body body { get; set; }
}
[XmlRoot(ElementName = "Header", Namespace = "http://www.w3.org/2003/05/soap-envelope")]
public class Header
{
[XmlElement(ElementName = "Action", Namespace = "http://www.w3.org/2005/08/addressing")]
public Action action { get; set; }
}
[XmlRoot(ElementName = "Action", Namespace = "http://www.w3.org/2005/08/addressing")]
public class Action
{
[XmlAttribute(AttributeName = "mustUnderstand", Namespace = "http://www.w3.org/2003/05/soap-envelope")]
public int mustUnderstand { get; set;}
[XmlText]
public string value { get;set;}
}
[XmlRoot(ElementName = "Body", Namespace = "")]
public class Body
{
[XmlElement(ElementName = "BatchResponse", Namespace = "https://example.com/operations")]
public BatchResponse batchResponse { get; set; }
}
[XmlRoot(ElementName = "BatchResponse", Namespace = "https://example.com/operations")]
public class BatchResponse
{
[XmlElement(ElementName = "BatchResult", Namespace = "https://example.com/operations")]
public BatchResult batchResult { get; set; }
}
[XmlRoot(ElementName = "BatchResult", Namespace = "https://example.com/operations")]
public class BatchResult
{
[XmlElement(ElementName = "FailureMessages", Namespace = "https://example.com/responses")]
public string failureMessages { get; set; }
[XmlElement(ElementName = "FailureType", Namespace = "https://example.com/responses")]
public string failureType { get; set; }
[XmlElement(ElementName = "ProcessedSuccessfully", Namespace = "https://example.com/responses")]
public Boolean processedSuccessfully { get; set; }
[XmlElement(ElementName = "SessionId", Namespace = "https://example.com/responses")]
public string sessionId { get; set; }
[XmlElement(ElementName = "TotalPages", Namespace = "https://example.com/responses")]
public int totalPages { get; set; }
}
}
you can use local-name() to ignore the namespace in xpath
//*[local-name()='Envelope']/*[local-name()='Body']/*[local-name()='BatchResponse']
Related
This is my xml string.
<test name="james" type="seoul" xmlns="test:client">
<friends xmlns="friends:test">
<friend xmlns="" name="john">
Google
</friend>
<friend xmlns="" name="john">
Amazon
</friend>
</friends>
</test>
And i tried this code behind.
[Serializable()]
[XmlRoot(ElementName = "test", Namespace = "test:client")]
public class TestModel
{
public TestModel()
{
}
[XmlAttribute("name")]
public string Name { get; set; }
[XmlAttribute("type")]
public string Id { get; set; }
}
I can never change the xml. and I want to make a list of contents in friends element.
please help me!!
Try following :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XmlReader reader = XmlReader.Create(FILENAME);
XmlSerializer serializer = new XmlSerializer(typeof(TestModel));
TestModel testModel = (TestModel)serializer.Deserialize(reader);
}
}
[XmlRoot(ElementName = "test", Namespace = "test:client")]
public class TestModel
{
[XmlAttribute("name")]
public string Name { get; set; }
[XmlAttribute("type")]
public string Id { get; set; }
[XmlArray(ElementName = "friends", Namespace = "friends:test")]
[XmlArrayItem("friend", Namespace = "")]
public List<Friend> friends { get; set; }
}
public class Friend
{
[XmlText]
public string text { get; set; }
}
}
please advice how do i map the response into class because from every example i came across body always don't have symbol like core:transactionResponse
my code:
string fileName = #"C:\Users\Lenovo\Downloads\GLResponseXml.xml";
XDocument xDoc = XDocument.Load(fileName);
var unwrappedResponse = xDoc.Descendants((XNamespace)"http://schemas.xmlsoap.org/soap/envelope/" + "Body")
.First()
.FirstNode;
XmlSerializer xmlSerializer = new XmlSerializer(typeof(TransactionResponse));
TransactionResponse response = (TransactionResponse)xmlSerializer.Deserialize(xDoc.Descendants((XNamespace)"http://schemas.xmlsoap.org/soap/envelope/" + "Body")
.First()
.FirstNode
.CreateReader()
);
for deserealizing this soap xml:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dpss0="bons">
<soapenv:Header/>
<soapenv:Body>
<core:transactionResponse xmlns:bo="http://service.example.co.id/core/bo" xmlns:core="http://service.example.co.id/core">
<response>
<header>
<coreJournal>149326</coreJournal>
</header>
<content xsi:type="bo:OKMessage">
<message/>
</content>
</response>
</core:transactionResponse>
</soapenv:Body>
</soapenv:Envelope>
but i got this error: InvalidOperationException: <transactionResponse xmlns='http://service.example.co.id/core'> was not expected.
i'm mapping the response into this class:
public class GLXmlResponse
{
[XmlRoot(ElementName = "response")]
public class Response
{
[XmlElement(ElementName = "header")]
public Header Header { get; set; }
[XmlElement(ElementName = "content")]
public Content Content { get; set; }
}
[XmlRoot(ElementName = "header")]
public class Header
{
[XmlElement(ElementName = "coreJournal")]
public string CoreJournal { get; set; }
}
[XmlRoot(ElementName = "content")]
public class Content
{
[XmlElement(ElementName = "message")]
public string Message { get; set; }
[XmlAttribute(AttributeName = "type", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
public string Type { get; set; }
}
[XmlRoot(ElementName = "transactionResponse", Namespace = "http://service.example.co.id/core")]
public class TransactionResponse
{
[XmlElement(ElementName = "response")]
public Response Response { get; set; }
[XmlAttribute(AttributeName = "bo", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Bo { get; set; }
[XmlAttribute(AttributeName = "core", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Core { get; set; }
}
[XmlRoot(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public class Body
{
[XmlElement(ElementName = "transactionResponse", Namespace = "http://service.example.co.id/core")]
public TransactionResponse TransactionResponse { get; set; }
}
[XmlRoot(ElementName = "Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public class Envelope
{
[XmlElement(ElementName = "Header", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public string Header { get; set; }
[XmlElement(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public Body Body { get; set; }
[XmlAttribute(AttributeName = "soapenv", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Soapenv { get; set; }
[XmlAttribute(AttributeName = "soapenc", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Soapenc { get; set; }
[XmlAttribute(AttributeName = "xsd", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Xsd { get; set; }
[XmlAttribute(AttributeName = "xsi", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Xsi { get; set; }
[XmlAttribute(AttributeName = "dpss0", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Dpss0 { get; set; }
}
}
need help on getting the class object right
I can't repro the exact error you're seeing, so you might want to check from a minimal repro; I'm using:
var env = (GLXmlResponse.Envelope)new XmlSerializer(typeof(GLXmlResponse.Envelope))
.Deserialize(new StringReader(xml));
System.Console.WriteLine(env.Body.TransactionResponse.Response.Header.CoreJournal);
where:
string xml = #"<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:soapenc=""http://schemas.xmlsoap.org/soap/encoding/"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:dpss0=""bons"">
<soapenv:Header/>
<soapenv:Body>
<core:transactionResponse xmlns:bo=""http://service.example.co.id/core/bo"" xmlns:core=""http://service.example.co.id/core"">
<response>
<header>
<coreJournal>149326</coreJournal>
</header>
<content xsi:type=""bo:OKMessage"">
<message/>
</content>
</response>
</core:transactionResponse>
</soapenv:Body></soapenv:Envelope>";
which just has a null for .Response. The reason for that is that this is actually in the empty namespace (prefixes are not inherited), so you need
[XmlElement(ElementName = "response", Namespace = "")]
public Response Response { get; set; }
However, this then causes a problem with the OKMessage in the xml. If I comment out the <content>, then it works and I can see the output 149326.
However, it might be simpler to start by just copying the xml into the clipboard, Edit -> Paste Special -> Paste XML As Classes, and see what it generates, and work from there.
I'm getting XML Data which contains an "envelope". For brevity, I'm focused on "Custom Fields" which is a collection of "Field".
Here is the part of the class definitions giving me a problem:
namespace Model.DocuSignEnvelope
{
[XmlRoot(ElementName = "CustomFields", Namespace = "http://www.docusign.net/API/3.0")]
public class CustomFields
{
[XmlElement(ElementName = "CustomField", Namespace = "http://www.docusign.net/API/3.0")]
public List<CustomField> CustomField { get; set; }
}
[XmlRoot(ElementName = "CustomField", Namespace = "http://www.docusign.net/API/3.0")]
public class CustomField
{
[XmlElement(ElementName = "Name", Namespace = "http://www.docusign.net/API/3.0")]
public string Name { get; set; } = "";
[XmlElement(ElementName = "Show", Namespace = "http://www.docusign.net/API/3.0")]
public string Show { get; set; } = "";
[XmlElement(ElementName = "Required", Namespace = "http://www.docusign.net/API/3.0")]
public string Required { get; set; } = "";
[XmlElement(ElementName = "Value", Namespace = "http://www.docusign.net/API/3.0")]
public string Value { get; set; } = "";
[XmlElement(ElementName = "CustomFieldType", Namespace = "http://www.docusign.net/API/3.0")]
public string CustomFieldType { get; set; } = "";
}
}
public static class DocuSignExtensionMethods
{
public static T DeserializeXmlX<T>(this string input) where T : class
{
XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(typeof(T));
using StringReader sr = new StringReader(input);
return (T)ser.Deserialize(sr);
}
}
if I get an empty list, happy times, it deserializes without issue:
<?xml version="1.0" encoding="utf-8"?>
<DocuSignEnvelopeInformation
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.docusign.net/API/3.0">
<EnvelopeStatus>
<RecipientStatuses>
<RecipientStatus>
<Type>Signer</Type>
...
<CustomFields />
...
but if I get a list of "empty" Custom Field
<?xml version="1.0" encoding="utf-8"?>
<DocuSignEnvelopeInformation
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.docusign.net/API/3.0">
<EnvelopeStatus>
<RecipientStatuses>
<RecipientStatus>
<Type>Signer</Type>
...
<CustomFields>
<CustomField />
<CustomField />
<CustomField />
</CustomFields>
...
It errors out with the Exception:
- ex {"There is an error in XML document (20, 7)."} System.Exception {System.InvalidOperationException}
- InnerException {"ReadElementContentAs() methods cannot be called on an element that has child elements. Line 20, position 7."} System.Exception {System.Xml.XmlException}
How can I tell the Deserializer to deserialize those fields as either an empty List (no valid entries) or a list with 3 empty CustomFields.
The following code works :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XmlReader reader = XmlReader.Create(FILENAME);
XmlSerializer serializer = new XmlSerializer(typeof(EnvelopeInfo));
EnvelopeInfo fields = (EnvelopeInfo)serializer.Deserialize(reader);
}
}
[XmlRoot(ElementName = "EnvelopeInfo", Namespace = "http://www.docusign.net/API/3.0")]
public class EnvelopeInfo
{
[XmlElement(ElementName = "EnvelopeStatus", Namespace = "http://www.docusign.net/API/3.0")]
public EnvelopeStatus EnvelopeStatus { get; set; }
}
public class EnvelopeStatus
{
[XmlArray(ElementName = "RecipientStatuses", Namespace = "http://www.docusign.net/API/3.0")]
[XmlArrayItem(ElementName = "RecipientStatus", Namespace = "http://www.docusign.net/API/3.0")]
public RecipientStatus[] RecipientStatus { get; set; }
}
public class RecipientStatus
{
[XmlElement(ElementName = "CustomFields", Namespace = "http://www.docusign.net/API/3.0")]
public CustomFields CustomFields { get; set; }
}
public class CustomFields
{
[XmlElement(ElementName = "CustomField", Namespace = "http://www.docusign.net/API/3.0")]
public List<CustomField> CustomField { get; set; }
}
[XmlRoot(ElementName = "CustomField", Namespace = "http://www.docusign.net/API/3.0")]
public class CustomField
{
[XmlElement(ElementName = "Name", Namespace = "http://www.docusign.net/API/3.0")]
public string Name { get; set; }
[XmlElement(ElementName = "Show", Namespace = "http://www.docusign.net/API/3.0")]
public string Show { get; set; }
[XmlElement(ElementName = "Required", Namespace = "http://www.docusign.net/API/3.0")]
public string Required { get; set; }
[XmlElement(ElementName = "Value", Namespace = "http://www.docusign.net/API/3.0")]
public string Value { get; set; }
[XmlElement(ElementName = "CustomFieldType", Namespace = "http://www.docusign.net/API/3.0")]
public string CustomFieldType { get; set; }
}
}
Used following XML
<?xml version="1.0" encoding="utf-8"?>
<EnvelopeInfo xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.docusign.net/API/3.0">
<EnvelopeStatus>
<RecipientStatuses>
<RecipientStatus>
<Type>Signer</Type>
<CustomFields>
<CustomField />
<CustomField />
<CustomField />
</CustomFields>
</RecipientStatus>
</RecipientStatuses>
</EnvelopeStatus>
</EnvelopeInfo>
I have the following XML class structure:
[XmlRoot(ElementName = "Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public class Envelope
{
[XmlElement(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public Body Body { get; set; }
}
[XmlRoot(ElementName = "Body")]
public class Body
{
[XmlElement(ElementName = "CustomerBundleMaintainConfirmation_sync_V1", Namespace = "http://sap.com/xi/SAPGlobal20/Global")]
public CustomerMaintainConfirmation CustomerMaintainConfirmation { get; set; }
}
[XmlRoot(ElementName = "CustomerBundleMaintainConfirmation_sync_V1")]
public class CustomerMaintainConfirmation
{
[XmlElement(ElementName = "Log")]
public Log Log { get; set; }
}
[XmlRoot(ElementName = "Log")]
public class Log
{
[XmlElement(ElementName = "MaximumLogItemSeverityCode")]
public string MaximumLogItemSeverityCode { get; set; }
[XmlElement(ElementName = "Item")]
public Item Item { get; set; }
}
[XmlRoot(ElementName = "Item")]
public class Item
{
[XmlElement(ElementName = "TypeID")]
public string TypeID { get; set; }
[XmlElement(ElementName = "CategoryCode")]
public string CategoryCode { get; set; }
[XmlElement(ElementName = "SeverityCode")]
public string SeverityCode { get; set; }
[XmlElement(ElementName = "Note")]
public string Note { get; set; }
}
And this is the XML I am working with:
<?xml version="1.0" encoding="UTF-8"?>
<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
<soap-env:Header />
<soap-env:Body>
<n0:CustomerBundleMaintainConfirmation_sync_V1 xmlns:n0="http://sap.com/xi/SAPGlobal20/Global" xmlns:prx="urn:sap.com:proxy:LBK:/1SAI/TAE6F3228CC6D723FF1823E:804">
<Log>
<MaximumLogItemSeverityCode>3</MaximumLogItemSeverityCode>
<Item>
<TypeID>018</TypeID>
<CategoryCode>YES</CategoryCode>
<SeverityCode>0</SeverityCode>
<Note>TestNotes</Note>
</Item>
</Log>
</n0:CustomerBundleMaintainConfirmation_sync_V1>
</soap-env:Body>
</soap-env:Envelope>
When I attempt to deserialize this data into my classes, for whatever reason the "Log" class is null. Envelope, Body and CustomerMaintainConfirmation are all populated correctly. I see no reason why this is the case, but I've been staring at this for so long as this point I'm absolutely sure I am missing a mistake in and amongst my code somewhere.
This last piece of code is inside of the method that does the actual deserializing:
XmlSerializer serializer = new XmlSerializer(typeof(Envelope));
using (StreamReader responseReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
return (Envelope)serializer.Deserialize(responseReader);
}
I'm still working through it, but if anyone could point out any issues they might see with what I've provided, please let me know.
It's a namespace inconsistency. It deserializes with this change:
[XmlRoot(ElementName = "CustomerBundleMaintainConfirmation_sync_V1")]
public class CustomerMaintainConfirmation
{
[XmlElement(ElementName = "Log", Namespace = "")]
public Log Log { get; set; }
}
You just miss the empty namespace indication on your element:
[XmlRoot(ElementName = "CustomerBundleMaintainConfirmation_sync_V1")]
public class CustomerMaintainConfirmation
{
[XmlElement(ElementName = "Log", Namespace = "")]
public Log Log { get; set; }
}
This is required, as apparently, your Log element has the empty namespace even though the CustomerBundleMaintainConfirmation_sync_V1 above has a nonempty one.
That's the only change in your code to make it work as shown in a fiddle.
I have following XML Structure:
<ComputationData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" DataType="DimX">
<Mode Name="M1">
<Area>
<Point PointNumber="3" Speed="127" Power="1455" Value="-2" />
<Point PointNumber="2" Speed="127" Power="1396.8" Value="2" />
<Point PointNumber="3" Speed="101.6" Power="1164" Value="-2" />
</Area>
</Mode>
</ComputationData>
In below class structure I am not able to get the value of Datatype which is available in XMl root(ComputationData).
For following XML I have created following class structure:
[Serializable]
[XmlRoot("ComputationData")]
public class FullLoadPerformanceGrid
{
/// <summary>
/// Name of the performance data type of this grid, e.g. Bsfc, tEat...
/// </summary>
[XmlElement(ElementName = "ComputationData", Type =
typeof(PerformanceType))]
public PerformanceType DataType { get; set; }
/// <summary>
/// List of available <see cref="FullLoadPerformanceMode"/>
/// </summary>
[XmlElement("Mode", Type = typeof(FullLoadPerformanceMode))]
public List<FullLoadPerformanceMode> Modes { get; set; }
}
[Serializable]
[XmlRoot("ComputationData")]
public class PerformanceType
{
public int Id { get; set; }
[DataMember(Name = "DataType")]
[XmlAttribute("DataType")]
public string DataType { get; set; }
public int Type { get; set; }
}
Can anyone help me with the class structure that how should I define the PerformanceType(DataType)?
Try following :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApplication3
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XmlReader reader = XmlReader.Create(FILENAME);
XmlSerializer serializer = new XmlSerializer(typeof(FullLoadPerformanceGrid));
FullLoadPerformanceGrid load = (FullLoadPerformanceGrid)serializer.Deserialize(reader);
}
}
[XmlRoot(ElementName = "ComputationData", Namespace = "")]
public class FullLoadPerformanceGrid
{
[XmlAttribute("DataType", Namespace = "")]
public string DataType { get; set; }
[XmlElement(ElementName = "Mode", Namespace = "")]
public Mode mode { get; set; }
}
[XmlRoot(ElementName = "Mode", Namespace = "" )]
public class Mode
{
[XmlAttribute("Name", Namespace = "")]
public string name { get; set; }
[XmlArray("Area", Namespace = "")]
[XmlArrayItem("Point", Namespace = "")]
public List<Point> points { get; set; }
}
[XmlRoot(ElementName = "Point", Namespace = "")]
public class Point
{
[XmlAttribute("PointNumber", Namespace = "")]
public int pointNumber { get; set; }
[XmlAttribute("Speed", Namespace = "")]
public decimal speed { get; set; }
[XmlAttribute("Power", Namespace = "")]
public decimal power { get; set; }
[XmlAttribute("Value", Namespace = "")]
public int value { get; set; }
}
}