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; }
}
}
Related
how to convert list obj to client.PostAsJsonAsync
My xml
<ListCustomer xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="tns:Chapter[1]">
<item xsi:type="tns:Chapter">
<custname xsi:type="xsd:string">sakutara</custname>
<gender xsi:type="xsd:string">nam</gender>
<dob xsi:type="xsd:string">21/06/1991</dob>
</item>
</ListCustomer>
My class model
public class item
{
public string custname { get; set; }
public string gender { get; set; }
public string dob { get; set; }
}
public class ListCustomer
{
public List<item> item { get; set; }
}
help me please ??
You are missing namespace so I modified the xml
<Soap xmlns:xsi="MyUrl" xmlns:SOAP-ENC="MyUrl">
<ListCustomer xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="tns:Chapter[1]">
<item xsi:type="tns:Chapter">
<custname xsi:type="xsd:string">sakutara</custname>
<gender xsi:type="xsd:string">nam</gender>
<dob xsi:type="xsd:string">21/06/1991</dob>
</item>
</ListCustomer>
</Soap>
Then I used following code :
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 FILENANE = #"c:\temp\test.xml";
static void Main(string[] args)
{
XmlReader reader = XmlReader.Create(FILENANE);
XmlSerializer serializer = new XmlSerializer(typeof(Soap));
Soap soap = (Soap)serializer.Deserialize(reader);
}
}
public class item
{
public string custname { get; set; }
public string gender { get; set; }
public string dob { get; set; }
}
public class ListCustomer
{
[XmlElement("item")]
public List<item> item { get; set; }
}
public class Soap
{
public ListCustomer ListCustomer { get; set; }
}
}
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 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']
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; }
}
}
I want to read an XML file with C# (unity project) in order to display some content. the XML file is given by a TALEND Job in Talend Studio.
I've converted an XML file to C# classes and I'm now trying to read this XML (stored in a file) to manage the content.
For the moment, I'm able to read some properties but the most interesting tag are nul. There is no error during the deserialization.
private const string filename = "/Applications/TalendStudio-7.1.1/studio/workspace/LOCAL_PROJECT/process/job1_0.1.item";
void Start()
{
// Create an instance of the XmlSerializer.
XmlSerializer serializer = new XmlSerializer(typeof(Xml2CSharp.ProcessType));
// Declare an object variable of the type to be deserialized.
Xml2CSharp.ProcessType i;
using (Stream reader = new FileStream(filename, FileMode.Open))
{
// Call the Deserialize method to restore the object's state.
i = (Xml2CSharp.ProcessType)serializer.Deserialize(reader);
Debug.Log(i);
}
}
the XML file starts with :
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<talendfile:ProcessType
xmlns:talendfile="TalendFile.xsd" xmlns:TalendMapper="http://www.talend.org/mapper" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
defaultContext="Default" jobType="Standard" xmi:version="2.0">
<context confirmationNeeded="false" name="Default">
<contextParameter comment="" name="max_rows" prompt="max_rows?" promptNeeded="false" type="id_Integer" value="10"/>
</context>
<context confirmationNeeded="false" name="prod">
<contextParameter comment="" name="max_rows" prompt="max_rows?" promptNeeded="false" type="id_Integer" value="30"/>
</context>
<context confirmationNeeded="false" name="qa">
<contextParameter comment="" name="max_rows" prompt="max_rows?" promptNeeded="false" type="id_Integer" value="20"/>
</context>
<parameters>
<elementParameter field="TEXT" name="SCREEN_OFFSET_X" show="false" value="0"/>
<elementParameter field="TEXT" name="SCREEN_OFFSET_Y" show="false" value="0"/>
<elementParameter field="TEXT" name="REPOSITORY_CONNECTION_ID" show="false" value=""/>
etc...
the 'context' and 'parameter' are empty. Thank you for your help.
Use xml serialization :
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(ProcessType));
ProcessType processType = (ProcessType)serializer.Deserialize(reader);
}
}
[XmlRoot(ElementName = "ProcessType", Namespace = "TalendFile.xsd")]
public class ProcessType
{
[XmlAttribute("defaultContext")]
public string defaultContext { get; set; }
[XmlAttribute("jobType")]
public string jobType { get; set; }
[XmlElement("context", Namespace = "")]
public List<Context> context { get; set; }
[XmlArray("parameters", Namespace = "")]
[XmlArrayItem("elementParameter", Namespace = "")]
public List<Parameters> parameters { get; set; }
}
[XmlRoot("context", Namespace = "")]
public class Context
{
[XmlAttribute("confirmationNeeded")]
public string confirmationNeeded { get; set; }
[XmlAttribute("name")]
public string name { get; set; }
[XmlElement("contextParameter", Namespace = "")]
public ContextParameter contextParameter { get; set; }
}
[XmlRoot("contextParameter", Namespace = "")]
public class ContextParameter
{
[XmlAttribute("comment")]
public string comment { get; set; }
[XmlAttribute("name")]
public string name { get; set; }
[XmlAttribute("prompt")]
public string prompt { get; set; }
[XmlAttribute("promptNeeded")]
public string promptNeeded { get; set; }
[XmlAttribute("type")]
public string type { get; set; }
[XmlAttribute("value")]
public int value { get; set; }
}
[XmlRoot("parameters", Namespace = "")]
public class Parameters
{
[XmlAttribute("field")]
public string field { get; set; }
[XmlAttribute("name")]
public string name { get; set; }
[XmlAttribute("show", Namespace = "")]
public string show { get; set; }
private int? _value { get; set; }
[XmlAttribute("value")]
public string value {
get { return _value.ToString(); }
set { _value = value == string.Empty ? (int?)null : int.Parse(value); }
}
}
}