Using xml to c# feature in visual studio converted the below xml markup into C# class.
<books>
<book name="Book-1">
<author>
<name>Author-1<ref>1</ref></name>
</author>
</book>
<book name="Book-2">
<author>
<name>Author-1<ref>1</ref></name>
</author>
</book>
</books>
The converted class contains
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class booksBookAuthorName
{
private byte refField; // 1
private string[] textField; // 2
What we need is Author-1<ref>1</ref>
as the output when reading the author name, by keeping tags.
We have tried https://msdn.microsoft.com/en-us/library/system.xml.serialization.xmltextattribute(v=vs.110).aspx attribute. But no hope.
Any clues ??
You can use
[XmlAnyElement("name")]
to capture the XML of the<name>
node of each author into anXmlElement
(orXElement
). The precise text you want is theInnerXml
of that element:This eliminates the need for the
booksBookAuthorName
class.Sample fiddle showing deserialization of a complete set of classes corresponding to your XML, generated initially from http://xmltocsharp.azurewebsites.net/ after which
Author
was modified as necessary.