xml-serialization

How can I deserialise an XML element into an array of elements with both attributes and text in C#?

▼魔方 西西 提交于 2020-01-14 09:54:08
问题 I am having a problem trying to deserialise this XML: <?xml version="1.0" encoding="UTF-8"?> <links> <link title="ABC">http://abc.co.uk</link> <link title="eBay">http://ebay.co.uk</link> <link title="Best Damn Site on the Web">http://stackoverflow.com</link> </links> Using the code: [XmlRoot("links")] public class LinksInterface { [XmlElement("link")] public List<LinkElement> Links; public class LinkElement { [XmlAttribute("title")] public string Title; [XmlText] // This bit is the

XmlSerialization of mutiple object types in the one list

北城以北 提交于 2020-01-13 19:57:27
问题 I have an object that has a list of abstract 'aninamls'. i.e. var animals = new Animals { new Bird{ TailFeatherColour = "Blue" }, new Cat{ Colour = "Brown" } }; using the xmlserializer, is it possible to serialize the above to the following xml, <?xml version="1.0" encoding="utf-16"?> <Animals> <Bird> <TailFeatherColour>Blue</TailFeatherColour> </Bird> <Cat> <Colour>Brown</Colour> </Cat> </Animals> at the moment, I can only get the following: <?xml version="1.0" encoding="utf-16"?> <Animals>

XmlSerialization of mutiple object types in the one list

孤人 提交于 2020-01-13 19:57:09
问题 I have an object that has a list of abstract 'aninamls'. i.e. var animals = new Animals { new Bird{ TailFeatherColour = "Blue" }, new Cat{ Colour = "Brown" } }; using the xmlserializer, is it possible to serialize the above to the following xml, <?xml version="1.0" encoding="utf-16"?> <Animals> <Bird> <TailFeatherColour>Blue</TailFeatherColour> </Bird> <Cat> <Colour>Brown</Colour> </Cat> </Animals> at the moment, I can only get the following: <?xml version="1.0" encoding="utf-16"?> <Animals>

Using XmlSerializer with private and public const properties

て烟熏妆下的殇ゞ 提交于 2020-01-13 09:50:12
问题 What's the simplest way to get XmlSerializer to also serialize private and "public const" properties of a class or struct? Right not all it will output for me is things that are only public. Making it private or adding const is causing the values to not be serialized. 回答1: XmlSerializer only looks at public fields and properties. If you need more control, you can implement IXmlSerializable and serialize whatever you would like. Of course, serializing a constant doesn't make much sense since

Why is Apache Xerces/Xalan adding additional carriage returns to my serialized output?

陌路散爱 提交于 2020-01-13 09:01:53
问题 I'm using Apache Xerces 2.11.0 and Apache Xalan 2.7.1 and I'm having problems with additional carriage return characters in the serialized XML. I have this (pseudo) code: String myString = ...; Document doc = ...; Element item = doc.createElement("item"); item.appendChild(doc.createCDATASection(myString)); Transformer transformer = ...; ByteArrayOutputStream stream = new ByteArrayOutputStream(); Result result = new StreamResult(stream); transformer.transform(new DOMSource(document), result);

XML serializing with XmlWriter via StringBuilder is utf-16 while via Stream is utf-8?

筅森魡賤 提交于 2020-01-11 18:04:21
问题 I was surprised when I encountered it, and wrote a console application to check it and make sure I wasn't doing anything else. Can anyone explain this? Here's the code: using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Xml; using System.Xml.Serialization; namespace ConsoleApplication1 { public class Program { static void Main(string[] args) { var o = new SomeObject { Field1 = "string value", Field2 = 8 }; Console.WriteLine(

XML serializing with XmlWriter via StringBuilder is utf-16 while via Stream is utf-8?

孤街醉人 提交于 2020-01-11 18:04:06
问题 I was surprised when I encountered it, and wrote a console application to check it and make sure I wasn't doing anything else. Can anyone explain this? Here's the code: using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Xml; using System.Xml.Serialization; namespace ConsoleApplication1 { public class Program { static void Main(string[] args) { var o = new SomeObject { Field1 = "string value", Field2 = 8 }; Console.WriteLine(

XML to C# class

耗尽温柔 提交于 2020-01-11 06:28:10
问题 Can anyone give me some advice? An API I'm consulting generates a pattern like this: <?xml version="1.0"?> <ChatXMLResult> <Generator>AppServer.network.lcpdfr.com</Generator> <Version>1000</Version> <Time>1305910998</Time> <Signature>a0f1f6bea66f75de574babd242e68c47</Signature> <FilteredResultSet>1</FilteredResultSet> <Messages> <Message> <ID>1</ID> <UID>9</UID> <DisplayName>Jay</DisplayName> <UserName>jaymac407</UserName> <Time>1305900497</Time> <Area>Masterson St</Area> <Message>Test<

How to add a line break when using XmlSerializer

孤街醉人 提交于 2020-01-10 20:24:28
问题 I am wondering how to add a line break for each element when using XmlSerializer? Sample code: XmlSerializer serializer = new XmlSerializer(typeof(xxx)); using (XmlWriter xmlWriter = XmlWriter.Create("test.xml") { serializer.Serialize(xmlWriter, xxx); } 回答1: When creating the XmlWriter , pass in an XmlWriterSettings object with Indent set to true . var xmlWriterSettings = new XmlWriterSettings() { Indent = true }; XmlSerializer serializer = new XmlSerializer(typeof(xxx)); using (XmlWriter

Convert an org.w3c.dom.Node into a String

人走茶凉 提交于 2020-01-10 17:21:51
问题 Sorry I'm a Java/XML newbie - and can't seem to figure this one out. It seems it's possible to convert a Document object to a string. However, I want to convert a Node object into a string. I am using org.ccil.cowan.tagsoup Parser for my purpose. I'm retrieving the Node by something like... parser = new org.ccil.cowan.tagsoup.Parser() parser.setFeature(namespaceaware, false) Transformer transformer = TransformerFactory.newInstance().newTransformer(); DOMResult domResult = new DOMResult();