[XmlType(Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public class Envelope
{
public Envelope() { }
public Body Body { get; set; }
}
[XmlType(Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public class Body
{
public Body() { }
public InfoDetails infoDetails { get; set; }
}
When I serialize those classes like following:
var myNamespaces = new XmlSerializerNamespaces();
myNamespaces.Add("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
//-- another code here
mySerializer.Serialize(myWriter, envelope, myNamespaces);
public class Program
{
static void Main(string[] args)
{
Program t = new Program();
// Write a purchase order.
t.SerializeObject(@"C:\Users\yangsh\Desktop\Files\simple.xml");
}
[XmlRoot(Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public class OrderedItem
{
[XmlElement(Namespace = "http://www.cpandl.com")]
public string ItemName;
[XmlElement(Namespace = "http://www.cpandl.com")]
public string Description;
[XmlElement(Namespace = "http://www.cohowinery.com")]
public decimal UnitPrice;
[XmlElement(Namespace = "http://www.cpandl.com")]
public int Quantity;
[XmlElement(Namespace = "http://www.cohowinery.com")]
public decimal LineTotal;
// A custom method used to calculate price per item.
public void Calculate()
{
LineTotal = UnitPrice * Quantity;
}
}
private void SerializeObject(string filename)
{
Console.WriteLine("Writing With TextWriter");
// Create an XmlSerializer instance using the type.
XmlSerializer serializer =
new XmlSerializer(typeof(OrderedItem));
OrderedItem i = new OrderedItem();
i.ItemName = "Widget";
i.Description = "Regular Widget";
i.Quantity = 10;
i.UnitPrice = (decimal)2.30;
i.Calculate();
// Create an XmlSerializerNamespaces object.
XmlSerializerNamespaces ns =
new XmlSerializerNamespaces();
// Add two namespaces with prefixes.
ns.Add("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
ns.Add("inventory", "http://www.cpandl.com");
ns.Add("money", "http://www.cohowinery.com");
// Create a StreamWriter to write with.
TextWriter writer = new StreamWriter(filename);
/* Serialize using the object using the TextWriter
and namespaces. */
serializer.Serialize(writer, i, ns);
writer.Close();
}
}
Member
117 Points
511 Posts
XmlSerializerNamespaces missing prefix
Feb 27, 2020 01:23 AM|KulerMaster|LINK
Hello,
I have the following classes to be serialized:
When I serialize those classes like following:
I end up with an XML like this:
As you can see the root element is without a prefix soapenv.
What I am doing wrong if I want the Envelope element to also has a prefix soapenv?
Thank you
Contributor
3140 Points
983 Posts
Re: XmlSerializerNamespaces missing prefix
Feb 27, 2020 04:35 AM|Yang Shen|LINK
Hi KulerMaster,
Instead of
XmlType
, you need to useXmlRoot
for yourEnvelope
class.I built a demo based on the code from Serialize(XmlWriter, Object, XmlSerializerNamespaces).
public class Program { static void Main(string[] args) { Program t = new Program(); // Write a purchase order. t.SerializeObject(@"C:\Users\yangsh\Desktop\Files\simple.xml"); } [XmlRoot(Namespace = "http://schemas.xmlsoap.org/soap/envelope/")] public class OrderedItem { [XmlElement(Namespace = "http://www.cpandl.com")] public string ItemName; [XmlElement(Namespace = "http://www.cpandl.com")] public string Description; [XmlElement(Namespace = "http://www.cohowinery.com")] public decimal UnitPrice; [XmlElement(Namespace = "http://www.cpandl.com")] public int Quantity; [XmlElement(Namespace = "http://www.cohowinery.com")] public decimal LineTotal; // A custom method used to calculate price per item. public void Calculate() { LineTotal = UnitPrice * Quantity; } } private void SerializeObject(string filename) { Console.WriteLine("Writing With TextWriter"); // Create an XmlSerializer instance using the type. XmlSerializer serializer = new XmlSerializer(typeof(OrderedItem)); OrderedItem i = new OrderedItem(); i.ItemName = "Widget"; i.Description = "Regular Widget"; i.Quantity = 10; i.UnitPrice = (decimal)2.30; i.Calculate(); // Create an XmlSerializerNamespaces object. XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); // Add two namespaces with prefixes. ns.Add("soapenv", "http://schemas.xmlsoap.org/soap/envelope/"); ns.Add("inventory", "http://www.cpandl.com"); ns.Add("money", "http://www.cohowinery.com"); // Create a StreamWriter to write with. TextWriter writer = new StreamWriter(filename); /* Serialize using the object using the TextWriter and namespaces. */ serializer.Serialize(writer, i, ns); writer.Close(); } }
The result - simple.xml:
<?xml version="1.0" encoding="utf-8"?> <soapenv:OrderedItem xmlns:money="http://www.cohowinery.com" xmlns:inventory="http://www.cpandl.com" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <inventory:ItemName>Widget</inventory:ItemName> <inventory:Description>Regular Widget</inventory:Description> <money:UnitPrice>2.3</money:UnitPrice> <inventory:Quantity>10</inventory:Quantity> <money:LineTotal>23.0</money:LineTotal> </soapenv:OrderedItem>
Best Regard,
Yang Shen