List Collection XML Serialization - c#

I've been playing around with the xml serialization for a while and I've hit a problem with serializing the a list collection. I want to serialize a list collection without the upper element wrapping around it. See example below:
Result serialization:
<?xml version="1.0" encoding="utf-8" ?>
<Person>
<Name>John</Name>
<AddressLine>
<string>Line 1</string>
<string>Line 2</string>
<string>Line 3</string>
</AddressLine>
<Telephone>123456789</Telephone>
</Person>
The serialization I want to output is:
<?xml version="1.0" encoding="utf-8" ?>
<Person>
<Name>John</Name>
<AddressLine>Line 1</AddressLine>
<AddressLine>Line 2</AddressLine>
<AddressLine>Line 3</AddressLine>
<Telephone>123456789</Telephone>
</Person>
I have tried setting different the attributes to my class I'm serializaing from but I can't seem to get anywhere with it. If anyone could show me what attributes I need to use to get my xml serialization to look like the ouput xml I want that would be greatly appreciated.
Cheers!

[Serializable]
public class Person
{
public string Name { get; set; }
[XmlElement]
public List<string> AddressLine { get; set; }
}
Produces desired output:
<?xml version="1.0"?>
<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Name>John</Name>
<AddressLine>1</AddressLine>
<AddressLine>2</AddressLine>
<AddressLine>3</AddressLine>
</Person>

Related

Change XML Tag Prefix SOAP

I am trying to create a SOAP message with the prefix. however, I am having trouble setting the namespace correctly. I have been trying for days and tried many suggestions I found online, but none seem to work. I am hoping some of you can help me.
What I'm getting is
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<Transaction xmlns="http://tempuri.org/">
<bankingTransaction>
<operation parameterOrder="string">
<fault />
<fault />
</operation>
<transactionDate>dateTime</transactionDate>
<amount>int</amount>
</bankingTransaction>
</Transaction>
</soap:Body>
</soap:Envelope>
& what I actually need is
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<res:Transaction xmlns="res:http://tempuri.org/">
<res:bankingTransaction>
<res:operation parameterOrder="string">
<res:fault />
<res:fault />
</res:operation>
<res:transactionDate>dateTime</res:transactionDate>
<res:amount>int</res:amount>
</res:bankingTransaction>
</res:Transaction>
</soap:Body>
</soap:Envelope>
& My MassageContact is
[MessageContract]
public class BankingTransaction
{
[MessageHeader] public Operation operation;
[MessageHeader] public DateTime transactionDate;
[MessageBodyMember] private unit sourceAccount;
[MessageBodyMember] public int amount;
}
Please Help me to add prefix with my XML Elements.
Thanks
You probably need to do something like this:
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://tempuri.org/")]
[MessageContract]
public class BankingTransaction
{
[MessageHeader] public Operation operation;
[MessageHeader] public DateTime transactionDate;
[MessageBodyMember] private unit sourceAccount;
[MessageBodyMember] public int amount;
}
I am not sure how you are serializing your objects, but something like this will add the prefix:
XmlSerializerNamespaces x = new XmlSerializerNamespaces();
x.Add("res", "http://tempuri.org/");
add the XmlSerializerNamespaces to you serialization process maybe? It's hard to say without seeing what else you are doing. All your contracts/classes in that namespace probably need this attribute: [System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://tempuri.org/")]
We could create a MessageFormatter to customize the message format, you could refer to the following official tutorial.
https://learn.microsoft.com/en-us/dotnet/framework/wcf/extending/custom-message-formatters
Here is an example about how to do this.
https://stackoverflow.com/questions/31595770/c-sharp-wcf-global-namespaces-royal-mail/31597758#31597758
http://vanacosmin.ro/Articles/Read/WCFEnvelopeNamespacePrefix

NServiceBus generates XML message without simple type namespaces

I'm currently upgrading from NServiceBus 4.7.5 to NServiceBus 5.2.4, and when using the XmlSerializer, I noticed that it generates an invalid XML message when I try to serialize a message -
the namespaces for the simple types are no longer declared, but they're still used in the document.
For example, if I try to serialize a message that exposes a SerializedPair property that is defined as follows:
public class SerializedPair
{
public string Key { get; set; }
public object Value { get; set; }
}
in 4.7.5 it would be serialized as:
<?xml version="1.0"?>
<Messages xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://tempuri.net/xxx.Crm.ServiceBusMessages"
xmlns:guid="Guid" xmlns:int32="Int32"
xmlns:string="String"
xmlns:datetime="DateTime"
xmlns:boolean="Boolean"
xmlns:decimal="Decimal">
<UpdateContact>
<SerializedPair>
<Key>AddressId</Key>
<guid:Value>ebdeeb33-baa7-4100-b1aa-eb4d6816fd3d</guid:Value>
</SerializedPair>
....
In 5.2.4, it gets serialized as:
<?xml version="1.0" ?>
<UpdateContact xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://tempuri.net/xxx.Crm.ServiceBusMessages">
<SerializedPair>
<Key>AddressId</Key>
<guid:Value>70a22cd7-64fd-4d6d-ab13-2ad7800addc7</guid:Value>
</SerializedPair>
....
Note that the "Messages" element is no longer present, and the "xmlns:guid" namespace declaration is also missing, which makes this invalid XML.
How do I get NServiceBus to generate valid xml again?
For completeness, my BusConfiguration looks like:
BusConfiguration busConfiguration = new BusConfiguration();
busConfiguration.UseSerialization<XmlSerializer>();
busConfiguration.UseTransport<MsmqTransport>();
ISendOnlyBus bus = Bus.CreateSendOnly(busConfiguration);

How to deserialise a named XML node and grab all of its children C#

I've the following XML which I can deserialise in C#:
<?xml version="1.0" encoding="iso-8859-1" ?>
<ProductList>
<Product>
<MyDesc><![CDATA[DOCTOR WHO - PLANET OF THE DEA]]></MyDesc>
<ActualStock><![CDATA[5]]></ActualStock>
</Product>
</ProductList>
However, if I add a new node eg MetaData, I do not know how to grab that node and all of its children as text:
<?xml version="1.0" encoding="iso-8859-1" ?>
<ProductList>
<Product>
<MyDesc><![CDATA[DOCTOR WHO - PLANET OF THE DEA]]></MyDesc>
<ActualStock><![CDATA[5]]></ActualStock>
**<MetaData>
<Desc>MD-Description - This is my product desc</Desc>
<Image>MD - The main image</Image>
</MetaData>**
</Product>
</ProductList>
ie I want to grab and store everything within MetaData as one string. The trouble is the children of MetaData are unknown.
Is this possible?
I thought the deserialiser would use this to match the MetaData node:
[XmlElement("MetaData")]
public List<MetaData> MetaDataList
{
get
{
return metaDataList;
}
set
{
metaDataList = value;
}
}
and then figure out MetaData's children using:
private List<MetaData> metaDataList = new List<MetaData>();
public class MetaData
{
[XmlAnyElement]
public List<string> Children
{
get;
set;
}
}
As you can see I'm completely stuck...
Pointers would be appreciated.
Thanks.
Sai.

XML Serialize and Deserialize Issue for Complicated XML

My XML
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ServerConfig loggingEnabled="1">
<Servers id="2" host="mytest">
<Server host="test1.example.com" port="9999" >
<Name id="newname1">
<FirstName id="myfirstname">hello first name</FirstName>
<SecondName id="myfirstname">hello first name</SecondName>
</Name>
</Server>
<Server host="test2.example.com" port="8888" />
</Servers>
<Servers id="1">
<Server host="test3.example.com" port="9899" >
<Name id="newname2">
<FirstName id="myfirstname">hello first name</FirstName>
</Name>
</Server>
<Server host="test4.example.com" port="8988" />
</Servers>
</ServerConfig>
I want to deserialize this xml to my class
Class
public sealed class ServerConfig
{
public sealed class Server
{
[XmlAttribute("host")]
public string Host { get; set; } // gives me host name
[XmlAttribute("port")]
public int Port { get; set; } // gives my prot number
}
[XmlArray]
public List<Server> Servers { get; set; } // gives me all 4 server lsit
[XmlAttribute("loggingEnabled")]
public int LoggingEnabled { get; set; } // gives me attribute detail
public ServerConfig()
{
Servers = null;
LoggingEnabled = 0;
}
}
Problem
My Problem is I don't know how to access the Attributes of the nested Element Name and sub nested element FirstName/SecondName
WIll be really very thankful.
Thanks.
The simplest way is being provided by Microsoft in VS 2012 onwards which is being brilliantly explained in this link.
XML TO C# Class
All you need to do is copy your xml and then paste special to a class as explained in the link.
Happy Coding.
Thanks,

XmlSerialization in C# - root element array

I would like to de/serialize a XML-Document with type=array as root node.
The given xml structure looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<parties type="array">
<party type="Person">
<id>1</id>
<lastname>Smith</lastname>
<firstname>Peter</firstname>
...
</party>
<party type="Person">
<id>2</id>
<lastname>Smith</lastname>
<firstname>Sarah</firstname>
...
</party>
<parties type="array">
C# Code looks like this:
[XmlRootAttribute("parties", Namespace = "", IsNullable = false)]
public class Parties
{
private ArrayList contacts = new ArrayList();
public Parties()
{
}
[XmlArray("parties"), XmlArrayItem("party", typeof(Person))]
public ArrayList Contacts
{
get { return contacts; }
set { contacts = value; }
}
}
The resulting xml output is this:
<?xml version="1.0" encoding="utf-8"?>
<parties xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<parties>
<party>
<id>0</id>
<lastname>Smith</last-name>
<firstname>Peter</first-name>
</party>
</parties>
</parties>
The problem is that I have 2 -tags now.
How can I specify array type for the root element? Any ideas how to fix it without changing the given xml schema?
Try this:
[XmlElement("party")]
public ArrayList Contacts
{
get { return contacts; }
set { contacts = value; }
}

Categories