xmlserializer

Deserialize object property with StringReader vs XmlNodeReader

↘锁芯ラ 提交于 2019-11-27 05:39:31
Why does XmlSerializer populate my object property with an XmlNode array when deserializing an empty typed element using XmlNodeReader instead of an empty string like it does when using StringReader (or XmlTextReader )? The second assertion in the following code sample fails: var doc = new XmlDocument(); doc.Load(new StringReader(@" <Test xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema""> <Value xsi:type=""xsd:string"" /> </Test>")); var ser = new XmlSerializer(typeof (Test)); var reader1 = new StringReader(doc.InnerXml); var obj1 = (Test)

Using XmlSerializer to create an element with attributes and a value but no sub-element

天涯浪子 提交于 2019-11-27 05:23:51
Hopefully this should be an easy answer for someone out there (and possibly a dupe), but I can't seem to figure it out. I need to output an element that looks like this: <Quantity foo="AB" bar="CD">37</Quantity> I know how to get this: <Quantity foo="AB" bar="CD"> <qty>37</qty> </Quantity> with a Quantity class containing public int qty; [XmlAttribute] public string foo; [XmlAttribute] public string bar; but then of course whatever variable I insert the quantity into becomes its own sub-element. On the other hand, if I make the Quantity a variable in the parent element, then I can set the

Cannot deserialize XML into a list using XML Deserializer

久未见 提交于 2019-11-27 04:53:57
问题 This follows on from my previous question Serialize list of interfaces using XML Serialization public class MeterWalkOrder { public MeterWalkOrder() { Meters = new List<IMeter>(); } public String Name { get; set; } [XmlIgnore] public List<IMeter> Meters { get; set; } [XmlArrayItem(ElementName = "Meter")] [XmlArray(ElementName = "Meters")] public List<Meter> SerializableMeters { get { return Meters.Cast<Meter>().ToList(); } set { Meters = new List<IMeter>(value); } } } public interface IMeter

Rename class when serializing to XML

自闭症网瘾萝莉.ら 提交于 2019-11-27 04:47:02
问题 I'm trying to serialize the Outer class shown below, and create an XElement from the serialized XML. It has a property which is of type Inner . I'd like to change the name of both Inner (to Inner_X ) and Outer (to Outer_X ). class Program { static void Main(string[] args) { using (MemoryStream memoryStream = new MemoryStream()) { using (TextWriter streamWriter = new StreamWriter(memoryStream)) { var xmlSerializer = new XmlSerializer(typeof(Outer)); xmlSerializer.Serialize(streamWriter, new

How to write a comment to an XML file when using the XmlSerializer?

∥☆過路亽.° 提交于 2019-11-27 04:05:18
I have an object Foo which I serialize to an XML stream. public class Foo { // The application version, NOT the file version! public string Version {get;set;} public string Name {get;set;} } Foo foo = new Foo { Version = "1.0", Name = "Bar" }; XmlSerializer xmlSerializer = new XmlSerializer(foo.GetType()); This works fast, easy and does everything currently required. The problem I'm having is that I need to maintain a separate documentation file with some minor remarks. As in the above example, Name is obvious, but Version is the application version and not the data file version as one could

C# Xml Serializing List<T> descendant with Xml Attribute

自作多情 提交于 2019-11-27 03:55:05
问题 Morning Guys, I have a collection that descends from List and has a public property. The Xml serializer does not pick up my proeprty. The list items serialize fine. I have tried the XmlAttribute attribute to no avail. Do you guys have a solution? public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void button1_Click(object sender, RoutedEventArgs e) { var people = new PersonCollection { new Person { FirstName="Sue", Age=17 }, new Person {

Deserializing List<int> with XmlSerializer Causing Extra Items

天大地大妈咪最大 提交于 2019-11-26 23:28:07
问题 I'm noticing an odd behavior with the XmlSerializer and generic lists (specifically List<int> ). I was wondering if anyone has seen this before or knows what's going on. It appears as though the serialization works fine but the deserialization wants to add extra items to the list. The code below demonstrates the problem. Serializable class: public class ListTest { public int[] Array { get; set; } public List<int> List { get; set; } public ListTest() { Array = new[] {1, 2, 3, 4}; List = new

FileMode.Open and FileMode.OpenOrCreate difference when file exists? c# bug?

懵懂的女人 提交于 2019-11-26 23:06:30
I have wrote that code: public void Save() { using (FileStream fs = new FileStream(Properties.Settings.Default.settings_file_path, FileMode.Open)) { XmlSerializer ser = new XmlSerializer(typeof(MySettings)); ser.Serialize(fs, this); } } When I am using FileMode.Open everything is good, and output is e.x. like this: <?xml version="1.0"?> <MySettings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <settingsList> <Setting> <Value>12</Value> <Name>A0</Name> <Type>MEASUREMENT</Type> </Setting> <Setting> <Value>5000</Value> <Name>C0</Name> <Type

How to XML-serialize a dictionary

微笑、不失礼 提交于 2019-11-26 22:52:08
I have been able to serialize an IEnumerable this way: [XmlArray("TRANSACTIONS")] [XmlArrayItem("TRANSACTION", typeof(Record))] public IEnumerable<BudgetRecord> Records { get { foreach(Record br in _budget) { yield return br; } } } However, I realised that now I need a dictionary containing a collection Dictionary<string, RecordCollection> (RecordCollection implements IEnumerable). How can I achieve that? Take a look at the following blog post http://blogs.msdn.com/b/psheill/archive/2005/04/09/406823.aspx http://web.archive.org/web/20100703052446/http://blogs.msdn.com/b/psheill/archive/2005/04

XmlSerializer serialize generic List of interface

拜拜、爱过 提交于 2019-11-26 22:41:58
I'm trying to use the XmlSerializer to persist a List(T) where T is an interface. The serializer does not like interfaces. I'm curious if there is a simple way to serialize a list of heterogeneous objects easily with XmlSerializer. Here's what I'm going for: public interface IAnimal { int Age(); } public class Dog : IAnimal { public int Age() { return 1; } } public class Cat : IAnimal { public int Age() { return 1; } } private void button1_Click(object sender, RoutedEventArgs e) { var animals = new List<IAnimal> { new Dog(), new Cat() }; var x = new XmlSerializer(animals.GetType()); var b =