xmlserializer

Converting Class to XML to string

柔情痞子 提交于 2020-01-04 05:10:11
问题 I'm using XMLSerializer to serialize a class into a XML. There are plenty of examples to this and save the XML into a file. However what I want is to put the XML into a string rather than save it to a file. I'm experimenting with the code below, but it's not working: public static void Main(string[] args) { XmlSerializer ser = new XmlSerializer(typeof(TestClass)); MemoryStream m = new MemoryStream(); ser.Serialize(m, new TestClass()); string xml = new StreamReader(m).ReadToEnd(); Console

In JavaScript, how can serializer part of the DOM to XHTML?

亡梦爱人 提交于 2020-01-03 13:58:17
问题 I would like to serialize part of the DOM to XHTML (valid XML). Let's assume I have just one element inside <body> , and that this is the element I want to serialize: <div> <hr> <img src="/foo.png"> </div> With this, document.innerHTML gives me almost what I want, except it returns HTML, not XHTML (i.e. the <hr> and <img> won't be properly closed). Since innerHTML doesn't do the trick, how can I serialize part of the DOM to XHTML? 回答1: I am not sure if using another language (on top of the

In JavaScript, how can serializer part of the DOM to XHTML?

送分小仙女□ 提交于 2020-01-03 13:58:03
问题 I would like to serialize part of the DOM to XHTML (valid XML). Let's assume I have just one element inside <body> , and that this is the element I want to serialize: <div> <hr> <img src="/foo.png"> </div> With this, document.innerHTML gives me almost what I want, except it returns HTML, not XHTML (i.e. the <hr> and <img> won't be properly closed). Since innerHTML doesn't do the trick, how can I serialize part of the DOM to XHTML? 回答1: I am not sure if using another language (on top of the

Unable to Deserialize XML in C# - InvalidOperationException

安稳与你 提交于 2020-01-02 09:54:34
问题 I have a C# app that has a custom section of configuration info in the App.config file. At this time, I am able to successfully load the custom info via code. However, I'm trying to load that same configuration info from a database as well. In an attempt to do this, I took a string of XML from my App.config file that I know is working. That string of XML looks like this: <departments> <department id="1" name="Sporting Goods"> <products> <product name="Basketball" price="9.99"> <add key="Color

Multiple namespaces with XmlSerializer

痞子三分冷 提交于 2020-01-02 05:47:25
问题 This is the scenario: I have nested classes and need to serialize then in an xml document [XmlRoot(Namespace="http://www.foo.bar/myschema")] public class root { [XmlAttribute] public string version { get; set; } [XmlElement] public child child { get; set; } ... } [XmlRoot(Namespace="http://www.foo.bar/myschema")] public class child { [XmlElement] public int elemA { get; set; } [XmlElement] public string elemB { get; set; } ... } I have created an method based in another example to remove

remove encoding from xmlserializer

一笑奈何 提交于 2019-12-31 21:57:12
问题 I am using the following code to create an xml document - XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", ""); new XmlSerializer(typeof(docket)).Serialize(Console.Out, i, ns); this works great in creating the xml file with no namespace attributes. i would like to also have no encoding attribute in the root element, but I cannot find a way to do it. Does anyone have any idea if this can be done? Thanks 回答1: Old answer removed and update with new solution: Assuming that

Ignore property of a property in Xml Serialization in .NET using XmlSerializer

感情迁移 提交于 2019-12-31 00:57:28
问题 I am carrying out Xml serrialization using XmlSerializer . I am carrying out serialization of ClassA , which contains property named MyProperty of type ClassB . I don't want a particular property of ClassB to be serialized. I have to use XmlAttributeOverrides as the classes are in another library. If the property was in ClassA itself, it would have been straightforward. XmlAttributeOverrides xmlOver = new XmlAttributeOverrides(); XmlAttributes xmlAttr = new XmlAttributes(); xmlAttr.XmlIgnore

Deserialization / model binding in MVC4 webapi does not work with arrays

半腔热情 提交于 2019-12-29 07:53:04
问题 I'm using the new WebApi which is part of MVC4 beta. I have the following class: public class Voucher { public string Id { get; set; } public string TableId { get; set; } public Product[] Products { get; set; } } My controller looks like this: public class VouchersController : ApiController { public Voucher PostVoucher(Voucher voucher) { //.... } } On the client side I serialize the data using an XmlSerializer . The output looks like expected and the Products array is serialized. If I post

How to serialize property of type Object with XmlSerializer

天大地大妈咪最大 提交于 2019-12-28 18:06:25
问题 I have a property: public object Tag but it can contain finite number of types, unfortunately without base type (except object type). But when I serialize the object with this property, it doesn't get serialized. Is there a way to instruct XmlSerializer with possible types? 回答1: I don't recommend this, but yes, you can use [XmlElement] etc to tell it about multiple candidate types for a member: public class Test { private static void Main() { var ser = new XmlSerializer(typeof (Test)); var

Removing invalid characters from XML before serializing it with XMLSerializer()

这一生的挚爱 提交于 2019-12-28 16:22:28
问题 I'm trying to store user-input in an XML document on the client-side (javascript), and transmit that to the server for persistence. One user, for example, pasted in text that included an STX character (0x2). The XMLSerializer did not escape the STX character, and therefore, did not serialize to well-formed XML. Or perhaps the .attr() call should have escaped the STX character, but in either case, invalid XML was produced. I'm finding the output of in-browser XMLSerializer() isn't always well