xmlserializer

Why does XmlSerializer throws an Exception and raise a ValidationEvent when a schema validation error occurs inside IXmlSerializable.ReadXml()

核能气质少年 提交于 2021-02-17 04:59:46
问题 I have written some tests for reading an XML file and validating it against an XSD schema. My data objects are using a mix of attribute based and custom IXmlSerializable implementation and I am using the XmlSerializer to perform deserialization. My test involves inserting an unknown element into the XML so that it does not conform to the schema. I then test if the validation event fires. If the unknown element is placed in the XML so it's a child of one of the attribute based data classes (i

Remove xml element with xsi:nil=“true” C# serialization

本小妞迷上赌 提交于 2021-02-09 10:58:20
问题 I have an XML which has some values and at times there can be null values as shown below: I do not want the nodes with null listed at all in the XML! The elements are set IsNullable = true in the class. Any suggestions as I have tried out many stuffs in Google.. nothing helped! <?xml version="1.0" encoding="utf-8"?> <Materials> <Material> <MaterialName>ABC</MaterialName> <Weight Value="0.303"> <Weight_A xsi:nil="true" /> <Weight_B xsi:nil="true" /> </Weight> <Density Value="800"> <Density_A

Remove xml element with xsi:nil=“true” C# serialization

若如初见. 提交于 2021-02-09 10:56:53
问题 I have an XML which has some values and at times there can be null values as shown below: I do not want the nodes with null listed at all in the XML! The elements are set IsNullable = true in the class. Any suggestions as I have tried out many stuffs in Google.. nothing helped! <?xml version="1.0" encoding="utf-8"?> <Materials> <Material> <MaterialName>ABC</MaterialName> <Weight Value="0.303"> <Weight_A xsi:nil="true" /> <Weight_B xsi:nil="true" /> </Weight> <Density Value="800"> <Density_A

Remove xml element with xsi:nil=“true” C# serialization

☆樱花仙子☆ 提交于 2021-02-09 10:56:21
问题 I have an XML which has some values and at times there can be null values as shown below: I do not want the nodes with null listed at all in the XML! The elements are set IsNullable = true in the class. Any suggestions as I have tried out many stuffs in Google.. nothing helped! <?xml version="1.0" encoding="utf-8"?> <Materials> <Material> <MaterialName>ABC</MaterialName> <Weight Value="0.303"> <Weight_A xsi:nil="true" /> <Weight_B xsi:nil="true" /> </Weight> <Density Value="800"> <Density_A

XML serializer returns null on object deserialization

北战南征 提交于 2021-02-08 19:42:18
问题 I have a stored procedure in the database which returns an XML stream, and the application deserializes that stream into its corresponding object. The stored procedure is defined like this (I simplified it to make it more readable): SELECT usrs.FirstName AS 'FirstName', usrs.LastName AS 'LastName', usrs.Username AS 'Username', usrs.DateJoined AS 'DateJoined' FROM USERS AS usrs WHERE usrs.Username = @username FOR XML PATH('UserProfile') Notice that Username is a primary key, so the stored

XML serializer returns null on object deserialization

岁酱吖の 提交于 2021-02-08 19:42:10
问题 I have a stored procedure in the database which returns an XML stream, and the application deserializes that stream into its corresponding object. The stored procedure is defined like this (I simplified it to make it more readable): SELECT usrs.FirstName AS 'FirstName', usrs.LastName AS 'LastName', usrs.Username AS 'Username', usrs.DateJoined AS 'DateJoined' FROM USERS AS usrs WHERE usrs.Username = @username FOR XML PATH('UserProfile') Notice that Username is a primary key, so the stored

Why is XmlSerializer so hard to use?

做~自己de王妃 提交于 2021-02-07 06:09:01
问题 I imagine to use XML serialization like this: class Foo { public Foo (string name) { Name1 = name; Name2 = name; } [XmlInclude] public string Name1 { get; private set; } [XmlInclude] private string Name2; } StreamWriter wr = new StreamWriter("path.xml"); new XmlSerializer<Foo>().Serialize (wr, new Foo ("me")); Edit: I know this code is wrong. It was just to display how I would like to use it. But this does not work at all: XmlSerializer is not generic. I have to cast from and to object on (de

Why is XmlSerializer so hard to use?

帅比萌擦擦* 提交于 2021-02-07 06:04:34
问题 I imagine to use XML serialization like this: class Foo { public Foo (string name) { Name1 = name; Name2 = name; } [XmlInclude] public string Name1 { get; private set; } [XmlInclude] private string Name2; } StreamWriter wr = new StreamWriter("path.xml"); new XmlSerializer<Foo>().Serialize (wr, new Foo ("me")); Edit: I know this code is wrong. It was just to display how I would like to use it. But this does not work at all: XmlSerializer is not generic. I have to cast from and to object on (de

Does XmlSerializer support property name changes (version tolerant)

会有一股神秘感。 提交于 2021-01-27 04:56:10
问题 I have public bool Included { get; set; } That I want to change to: public bool IsIncluded { get; set; } I want to have backward compatibility. I'd like to only have IsIncluded defined in code but being able to read old xml where the property would be "Included". Does XmlSerializer support it and how ? Note: I tried without success... (does not deserialize it) public bool IsIncluded { get; set; } [Obsolete] public bool Included { set { IsIncluded = value; } get { return IsIncluded; } } Update