xmlserializer

XML DeSerialize - Possible to Trap Extra XML in File?

笑着哭i 提交于 2019-12-02 15:46:30
问题 Is there a way to trap the extra XML tags in a file that you did not anticipate in your class? For Example: using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Xml.Serialization; namespace XmlDeserializerTest { class Program { static void Main(string[] args) { XmlSerializer deserializer = new XmlSerializer(typeof(PersonInfo)); StreamReader reader = new StreamReader(@"C:\XML\Xml.xml"); object obj = deserializer.Deserialize(reader)

.Net Add namespace to XML document as default and with a prefix

假装没事ソ 提交于 2019-12-02 14:58:13
问题 When using XMLSerializer to create an XML string of a serialization of a class oXML in vb.net as follows: Dim x As New Xml.Serialization.XmlSerializer(oXML.GetType, "urn:oecd:blah:blah") Dim xmlns = New XmlSerializerNamespaces() xmlns.Add(String.Empty, "urn:oecd:blah:blah") xmlns.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance") xmlns.Add("sfa", "urn:oecd:blah:blah1") xmlns.Add("iso", "urn:oecd:blah:blah2") xmlns.Add("ftc", "urn:oecd:blah:blah") Dim sw As New IO.StringWriter() x

How to ignore a specific List<T> item when serializing a container class

≡放荡痞女 提交于 2019-12-02 14:11:41
问题 I would like to know how to ignore a specific item/index of a List<T> from being serialized using XmlSerializer . For example, consider the following list: ... List<int> collection = new List<int>() {0, 1, 2}; ... What I would like to achieve is when serializing the above List<int> using XmlSerializer , I want the 0 to be ignored from being serialized, so the desired result is: ... <collection> <int>1</int> <int>2</int> </collection> // as you can see - there is no <int>0</int> value. ...

How to get an XML node value as string when deserializing

纵然是瞬间 提交于 2019-12-02 12:22:54
问题 I am sending a XML to a aspnet core web api. The value for the namespace prefix cfdi: is defined in a containing node: <cfdi:Comprobante> <cfdi:Conceptos> </cfdi:Conceptos> <cfdi:Addenda> <bfa2:AddendaBuzonFiscal version="2.0" xmlns:bfa2="http://www.buzonfiscal.com/ns/addenda/bf/2"><bfa2:TipoDocumento nombreCorto="FAC" descripcion="Factura"/><bfa2:CFD totalConLetra="CINCUENTA Y DOS MIL QUINIENTOS OCHENTA Y NUEVE PESOS 64/100 M.N." observaciones="OBSERVACIONES"/><bfa2:Extra atributo=

How to initialize XmlElement[]?

喜夏-厌秋 提交于 2019-12-02 11:32:44
问题 I have an auto generated proxy class which contains field XmlElement[] Any . In the protocol specification a variety of other types are allowed. How would I initialize this field? I might have, for example, something like: Any = new XmlElement[1]; Any[0] = new SomeRequestType().AsXmlElement() How would I make room for AsXmlElement in my code? public partial class AppDataType { private System.Xml.XmlElement[] anyField; private System.Xml.XmlAttribute[] anyAttrField; /// <remarks/> [System.Xml

XML DeSerialize - Possible to Trap Extra XML in File?

*爱你&永不变心* 提交于 2019-12-02 08:39:59
Is there a way to trap the extra XML tags in a file that you did not anticipate in your class? For Example: using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Xml.Serialization; namespace XmlDeserializerTest { class Program { static void Main(string[] args) { XmlSerializer deserializer = new XmlSerializer(typeof(PersonInfo)); StreamReader reader = new StreamReader(@"C:\XML\Xml.xml"); object obj = deserializer.Deserialize(reader); PersonInfo D = (PersonInfo) obj; Console.WriteLine(D.address.Age); reader.Close(); Console.ReadLine()

How to avoid encoding of special characters in xmlroot

柔情痞子 提交于 2019-12-02 08:03:24
I trying to serialize a class JTDChanges with different xmlrootname "ns1:BatchChanges" but after serializing when I write it into a file, "ns1:BatchChanges" is encoded to "ns1_x003A_BatchChanges". This is my class [ Serializable, XmlRoot("ns1:BatchChanges") ] public class JTDChanges { [XmlElement("OrgUnitChanges")] public List<OrgUnitStage> CustomerChanges = new List<OrgUnitStage>(); } Can anyone please suggest how can I avoid the encoding? I believe you are looking for the Xml Namespace functions [Serializable, XmlRoot("BatchChanges, Namespace = "http://www.w3.org/XML/2008/xsdl-exx/ns1") ]

.Net Add namespace to XML document as default and with a prefix

和自甴很熟 提交于 2019-12-02 07:46:56
When using XMLSerializer to create an XML string of a serialization of a class oXML in vb.net as follows: Dim x As New Xml.Serialization.XmlSerializer(oXML.GetType, "urn:oecd:blah:blah") Dim xmlns = New XmlSerializerNamespaces() xmlns.Add(String.Empty, "urn:oecd:blah:blah") xmlns.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance") xmlns.Add("sfa", "urn:oecd:blah:blah1") xmlns.Add("iso", "urn:oecd:blah:blah2") xmlns.Add("ftc", "urn:oecd:blah:blah") Dim sw As New IO.StringWriter() x.Serialize(sw, oXML, xmlns) You will notice that I have repeated the addition of namespace "urn:oecd:blah:blah"

XMLSerialize and EXCLUDE the base class

心不动则不痛 提交于 2019-12-02 07:42:27
问题 I would like to be able to XML serialize a subclass, but exclude its base class from the serialization. Here's my class (configured for DataObjects.NET): [Serializable] [HierarchyRoot] public class MyClass: Entity { [Field, Key] public int Id { get; private set; } [Field(Length = 20)] public string Name { get; set; } } Base class Entity cannot be serialized because it does not have a parameterless constructor. I won't be trying to reconstitute the object by deserializing (no Liskov issues). I

How to read XML file using XmlSerializer C# [duplicate]

孤街醉人 提交于 2019-12-02 07:35:55
This question already has an answer here: Deserialization of xml file by using XmlArray? 1 answer I have a problem when I want to read XML file by using XmlSerializer. My xml file like follow : <?xml version="1.0" encoding="utf-8"?> <contents> <section id="1"> <element1>2</element1> <element2>1</element2> <idx>1</idx> <idx>2</idx> <idx>3</idx> </section> <section id="2"> <element1>2</element1> <element2>1</element2> </section> <section id="3"/> </contents> Here are the classes: [Serializable()] public class section { [XmlAttribute("id")] public string id { get; set; } [XmlElement("element1")]