Reputation: 1080
I'm trying to follow a microsoft tutorial about XML serialization, but I getting some problems!!
This is XML file, used as input:
<?xml version="1.0" encoding="utf-8"?>
<Books xmlns:books="http://www.cpandl.com" xmlns:money="http://www.cohowinery.com">
<money:Book>
<books:TITLE>A Book Title</books:TITLE>
<money:PRICE books:currency="US Dollar">
<money:price>9.95</money:price>
</money:PRICE>
</money:Book>
</Books>
This is the class to bind the XML:
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;
}
}
This function read the XML into 'OrderedItem' class:
Console.WriteLine("Reading with Stream");
// Create an instance of the XmlSerializer.
var serializer = new XmlSerializer(typeof(OrderedItem));
// Reading the XML document requires a FileStream.
Stream reader = new FileStream(filename, FileMode.Open);
// Declare an object variable of the type to be deserialized.
// Call the Deserialize method to restore the object's state.
var i = (OrderedItem)serializer.Deserialize(reader);
Console.SetOut(new StreamWriter("a_output.xml"));
serializer.Serialize(Console.Out, i);
This is the XML after read and rewritten:
<?xml version="1.0" encoding="utf-8"?>
<OrderedItem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ItemName xmlns="http://www.cpandl.com">Widget</ItemName>
<Description xmlns="http://www.cpandl.com">Regular Widget</Description>
<UnitPrice xmlns="http://www.cohowinery.com">2.3</UnitPrice>
<Quantity xmlns="http://www.cpandl.com">10</Quantity>
<LineTotal xmlns="http://www.cohowinery.com">23</LineTotal>
</OrderedItem>
As you can see, the namespace are expanded. How should I write the output, to obtain the same XML with namespace label, instead of uri?
Upvotes: 5
Views: 596
Reputation: 100027
You need to add a member of type XmlSerializerNamespaces
, marked with a XmlNamespaceDeclarationsAttribute
:
public class OrderedItem
{
[XmlNamespaceDeclarations]
public XmlSerializerNamespaces xmlns = new XmlSerializerNamespaces();
...
}
And then add the namespace declarations when serializing:
OrderedItem item = new OrderedItem();
item.xmlns.Add("books", "http://www.cpandl.com");
item.xmlns.Add("money", "http://www.cohowinery.com");
XmlSerializer serializer = new XmlSerializer(typeof(OrderedItem));
...
Upvotes: 1
Reputation: 746
Check out the XmlSerializerNameSpaces
class: http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializernamespaces.aspx.
This example code should do the trick:
XmlSerializer s = new XmlSerializer(typeof(OrderedItem));
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("books", "http://www.cpandl.com");
ns.Add("money", "http://www.cohowinery.com");
s.Serialize(new StreamWriter("a_output.xml"), i, ns);
Upvotes: 2
Reputation: 6878
In order to serialize your object to the format provided above you you are going to have to implement the IXmlSerializable
interface on your object (MSDN Documentation). This interface allows you to implement methods that will give you complete control over the serialized result of your class (and also deserializing the xml back to your object).
This topic has been discussed here as well, look here for more information: Proper way to implement IXmlSerializable
Upvotes: 0
Reputation: 2741
You may want to have a look on the overloaded method for serializing an object:
defining namespaces for serialization
As mentioned there, you can define XmlSerializerNamespaces
with the following code lines:
// Create an XmlSerializerNamespaces object.
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
// Add two prefix-namespace pairs.
ns.Add("inventory", "http://www.cpandl.com");
ns.Add("money", "http://www.cohowinery.com");
Hope that helps.
Upvotes: 0