XML Serialization question - How to Serialize Element, Attribute and Text from One Object
-
I'm new into XML Serialization using .NET and after working with it for some time I'm quite fuzzled now. I can serialize elements with attributes containing other elements but how can I serialize something like <myElement name="foo">bar</myElement> I use a class for myElement with a XmlAttribute for the "name", but how to refer the value of the XML Element? Thanks in advance.
-
Answer:
http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmltextattribute.aspx, like so: using System; using System.Xml.Serialization; [Serializable, XmlRoot("myElement")] public class MyType { [XmlAttribute("name")] public string Name {get;set;} [XmlText] public string Text {get;set;} } static class Program { static void Main() { new XmlSerializer(typeof(MyType)).Serialize(Console.Out, new MyType { Name = "foo", Text = "bar" }); } }
Haiko Wick at Stack Overflow Visit the source
Related Q & A:
- How to get 'name' attribute of item of string-array?Best solution by Stack Overflow
- How can i get attribute from xpath?Best solution by Stack Overflow
- How to use Authorize Attribute in asp.net MVC?Best solution by Stack Overflow
- How can I transfer my address book from one Yahoo account to a new one?Best solution by Yahoo! Answers
- How can you edit a picture so that there is only color on one object?Best solution by lifehacker.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.