Last post Apr 08, 2016 05:59 AM by Zhi Lv - MSFT
Apr 07, 2016 02:20 AM|airic82|LINK
I'm not going to lie: I'm not that versed in serializing XML. I much prefer JSON, but since Apple only accepts XML for podcast, here I am.
Apple likes to use custom attributes that start with "itunes:" (ex: "itunes:category").
In my C# code, I have the following:
[System.Xml.Serialization.XmlElementAttribute("itunes:category", Namespace = "http://www.itunes.com/dtds/podcast-1.0.dtd")]
The problem is that the serializer outputs that as:
How do I tell it to keep the colon?
Right now, I'm using the XmlSerializer. If I switch to the default DataContractSerializer, it just ignores the "itunes:".
This will be the only XML I output. Everything else will be JSON, so if it's best to change some of the serializer settings, that's fine.
Sorry if this is a simple question and thanks in advance for the help!
Apr 07, 2016 08:59 AM|Zhi Lv - MSFT|LINK
From your description, I suppose you want to serialize object to xml file with namespace. If that is the case, you could refer to the following code:
static void Main()
public static void SerializeObject(string filename)
XmlSerializer s = new XmlSerializer(typeof(Books));
// Writing a file requires a TextWriter.
TextWriter t = new StreamWriter(filename);
/* Create an XmlSerializerNamespaces object and add two
prefix-namespace pairs. */
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
// Create a Book instance.
Book b = new Book();
b.TITLE = "A Book Title";
Price p = new Price();
p.price = (decimal)9.95;
p.currency = "US Dollar";
b.PRICE = p;
Books bks = new Books();
bks.Book = b;
s.Serialize(t, bks, ns);
The output as below:
<?xml version="1.0" encoding="utf-8"?>
<Books xmlns:books="http://www.cpandl.com" xmlns:money="http://www.cohowinery.com">
<books:TITLE>A Book Title</books:TITLE>
<money:PRICE books:currency="US Dollar">
More details, see:
Apr 07, 2016 12:51 PM|airic82|LINK
The one thing I didn't mention is that I'm trying to do this automatically with Web API. So my controller returns my object and Web API handles the serialization.
So I wouldn't be calling the serializer directly. Is the solution similar?
Apr 08, 2016 05:59 AM|Zhi Lv - MSFT|LINK
Is the solution similar?
I suppose it is similar.