rename property in xml schema class when serializing - c#

I have an auto generated c# class file from an xml schema using the xsd generator tool.
There is a property in this class that i need to rename from "Balance" to "balance" when the xml file gets created.
As this is a generated class i need to update the created xml object on the fly before seralizing so cant just add an atrribute over the class property with the expected name.
I have accomplished the task of ignoring certain properties by using the XmlAttributes class so am sure there is something i could do along same lines for this
Can anyone point me in the direction of how to achieve this?
Thanks

I have managed to resolve my issue by using the following:
var overrides = new XmlAttributeOverrides();
overrides.Add(typeof(MyGeneratedCustomType), "Balance", new XmlAttributes { XmlAttribute = new XmlAttributeAttribute("balance") });
var serializer = new XmlSerializer(xmlFile.GetType(), overrides);
MyGeneratedCustomType is a type that appears in the generated xsd class which holds the property i needed to rename. Its an elegant solution as there is very minimal code required.

Assuming you need to deserialize from XML like this:
<root>
<Balance />
</root>
and then serialize to XML like this:
<root>
<balance />
</root>
You have two options here:
You could just create a second class that mirrors your auto-generated class in every way except for having [XmlElement("balance")] on the Balance property in the mirrored class. If the generated class is XsdGenerated and the mirrored class is CustomClass, create a constructor or override the =s operator to be able to populate the CustomClass with all the fields from XsdGenerated. When you serialize CustomClass, you should get the desired result. I think this is the preferable option.
Implement IXmlSerializable on XsdGenerated. Call base() in the ReadXml method, and just have the WriteXml method create the balance tag in lower case. Note that this option is probably more challenging to write/maintain and would limit your ability to serialize and deserialize - it'd be a one way operation, unless you created an even more complex mechanism to set whether ReadXml() and WriteXml() should treat balance with an upper or lower case b.

Related

Serialize class properties with their types automatically

My boss have a strange request, he wants me to add a new function to serialize and deserialize all our products classes and add in the XML file all their property's types automatically.
I can't modify the classes to add new "types properties" before every "real property".
Is there a way to do this with [XmlAttributes] or something else ?
Thank you.
Maybe this is what you are looking for https://learn.microsoft.com/dotnet/api/system.xml.serialization.xmltypeattribute?view=netcore-3.1
this article describes the XmlTypeAttribute, which you can use to add a type attribute (in xml) on your properties tag.
[XmlType("aType")]
public string MyProperty {get;set;}

stricter XmlSerializer which does not allow unused nodes

I generated C# classes based on XSD using the xsd.exe tool from the SDK. Then I can use that class to [de]serialize objects using XmlSerializer... However the serializer seems to be very forgiving.
Is it possible that I can make the serializer throw an exception in case there is missing property or a "strange" XML node?
I think one way is to modify the setter of the property and make it validate the data (or use XSD validation)... However is there any other alternative solution for this problem ?
You can implement the IXmlSerializable interface and in the ReadXml method implementation, check for the specific elements that you require, throwing exceptions when you don't find them (or setting whatever notification you need to).
If you want to use a schema for validation (to use the minOccurs and maxOccurs schema attributes, for example), then you can configure the XmlReader instance to validate against the schema by setting the Schemas property on the XmlReaderSettings class that you pass to the Create method (note there are overloads of Create which take a TextReader, etc.).

Parsing an XML file -options?

I'm developing a system to pick up XML attachments from emails, via Exchange Web Services, and enter them into a DB, via a custom DAL object that I've created.
I've manage to extract the XML attachment and have it ready as a stream... they question is how to parse this stream and populate a DAL object.
I can create an XMLTextReader and iterate through each element. I don't see any problems with this other than that I suspect there is a much slicker way. The reader seems to treat the opening tag, the content of the tag and the closing tag as different elements (using reader.NodeType). I expected myValue to be considered one element rather than three. Like I said, I can get round this problem, but I'm sure there must be a better way.
I came across the idea of using an XML Serializer (completely new to me) but a quick look suggested that these can't handle ArrayLists and List (I'm using List).
Again, I'm new to LINQ, but LINQ-to-XML has also been mentioned, but examples I've seen seem rather complex - though that my simply be my lack of familiarity.
Basically, I don't want a cludged system, but I don't want to use any complicated technique with a learning curve, just because it's 'cool'.
What is the simplest and most effective way of translating this XML/Stream in to my DAL objects?
XML Sample:
<?xml version="1.0" encoding="UTF-8"?>
<enquiry>
<enquiryno>100001</enquiryno>
<companyname>myco</companyname>
<typeofbusiness>dunno</typeofbusiness>
<companyregno>ABC123</companyregno>
<postcode>12345</postcode>
<contactemail>me#example.com</contactemail>
<firstname>My</firstname>
<lastname>Name</lastname>
<vehicles>
<vehicle>
<vehiclereg>54321</vehiclereg>
<vehicletype>Car</vehicletype>
<vehiclemake>Ford</vehiclemake>
<cabtype>n/a</cabtype>
<powerbhp>130</powerbhp>
<registrationdate>01/01/2003</registrationdate>
</vehicle>
</vehicles>
</enquiry>
Update 1:
I'm trying to deserialize, based on Graham's example. I think I've set up the DAL for serialization, including specifying [XmlElement("whatever")] for each property. And I've tried to deserialize using the following:
SalesEnquiry enquiry = null;
XmlSerializer serializer = new XmlSerializer(typeof(SalesEnquiry));
enquiry = (SalesEnquiry)serializer.Deserialize(stream);
However, I get an exception:'There is an error in XML document (2, 2)'. The innerexception states {"<enquiry xmlns=''> was not expected."}
Conclusion (updated):
My previous problem was the fact that the element in the XML file (Enquiry) != the name of the class (SalesEnquiry). Rather than an [XmlElement] attribute for the class, we need an [XmlRoot] attribute instead. For completeness, if you want a property in your class to be ignored during serialization, you use the [XmlIgnore] attribute.
I've successfully serialized my object, and have now successfully taken the incoming XML and de-serialized it into a SalesEnquiry object.
This approach is far easier than manually parsing the XML. OK, there has been a steep learning curve, but it was worth it.
Thanks!
If your XML uses a schema (i.e. you're always going to know what elements appear, and where they appear in the tree), you could use XmlSerializer to create your objects. You'd just need some attributes on your classes to tell the serializer what XML elements or attributes they correspond to. Then you just load up your XML, create a new XmlSerializer with the type of the .NET object you want to create, and call the Deserialize method.
For example, you have a class like this:
[Serializable]
public class Person
{
[XmlElement("PersonName")]
public string Name { get; set; }
[XmlElement("PersonAge")]
public int Age { get; set; }
[XmlArrayItem("Child")]
public List<string> Children { get; set; }
}
And input XML like this (saved in a file for this example):
<?xml version="1.0"?>
<Person>
<PersonName>Bob</PersonName>
<PersonAge>35</PersonAge>
<Children>
<Child>Chris</Child>
<Child>Alice</Child>
</Children>
</Person>
Then you create a Person instance like this:
Person person = null;
XmlSerializer serializer = new XmlSerializer(typeof(Person));
using (FileStream fs = new FileStream(GetFileName(), FileMode.Open))
{
person = (Person)serializer.Deserialize(fs);
}
Update:
Based on your last update, I would guess that either you need to specify an XmlRoot attribute on the class that's acting as your root element (i.e. SalesEnquiry), or the XmlSerializer might be a bit confused that you're referencing an empty namespace in your XML (xmlns='' doesn't seem right).
XmlSerializer does support arrays & lists... as long as the contained type is serializable.
I have found Xsd2Code very helpful for this kind of thing: http://xsd2code.codeplex.com/
Basically, all you need to do is write an xsd file (an XML schema file) and specify a few command line switches. Xsd2Code will automatically generate a C# class file that contains all the classes and properties plus everything needed to handle the serialization. It's not a perfect solution as it doesn't support all aspects of XSD, but if your XML files are relatively simple collections of elements and attributes, it should be a nice short-cut for you.
There's another similar project on Codeplex called Linq to XSD (http://linqtoxsd.codeplex.com/), which was designed to enforce the entire XSD specification, but last time I checked, it was no longer being supported and not really ready for prime time. Thought it was worth a mention, though.

XmlDocument.Validate ignore properties without [XmlIgnore]

I have a object that has a number of properties that aren't present in the xsd file. When doing XmlDocument.Validate is there a way I can tell it to ignore properties that are not present in the xsd and instead just ensure that the properties required by xsd are present in the xml document?
I can get around this by adding [XmlIgnore] attributes all over my class but I would rather accomplish this by convention rather then explicitly add attributes all throughout my object model.
I doubt there is. Personally I would create a separate DTO, as it sounds like you're trying to make one object serve two jobs. Another option would be to use the XmlSerializer ctor that allows you to specify attribs at runtime, but this is much more work than [XmlIgnore].
So if you just want it to work: [XmlIgnore]. If you want it to be "pure", create a second DTO model and translate between them.

Deserializing XML to Objects in C#

So I have xml that looks like this:
<todo-list>
<id type="integer">#{id}</id>
<name>#{name}</name>
<description>#{description}</description>
<project-id type="integer">#{project_id}</project-id>
<milestone-id type="integer">#{milestone_id}</milestone-id>
<position type="integer">#{position}</position>
<!-- if user can see private lists -->
<private type="boolean">#{private}</private>
<!-- if the account supports time tracking -->
<tracked type="boolean">#{tracked}</tracked>
<!-- if todo-items are included in the response -->
<todo-items type="array">
<todo-item>
...
</todo-item>
<todo-item>
...
</todo-item>
...
</todo-items>
</todo-list>
How would I go about using .NET's serialization library to deserialize this into C# objects?
Currently I'm using reflection and I map between the xml and my objects using the naming conventions.
Create a class for each element that has a property for each element and a List or Array of objects (use the created one) for each child element. Then call System.Xml.Serialization.XmlSerializer.Deserialize on the string and cast the result as your object. Use the System.Xml.Serialization attributes to make adjustments, like to map the element to your ToDoList class, use the XmlElement("todo-list") attribute.
A shourtcut is to load your XML into Visual Studio, click the "Infer Schema" button and run "xsd.exe /c schema.xsd" to generate the classes. xsd.exe is in the tools folder. Then go through the generated code and make adjustments, such as changing shorts to ints where appropriate.
Boils down to using xsd.exe from tools in VS:
xsd.exe "%xsdFile%" /c /out:"%outDirectory%" /l:"%language%"
Then load it with reader and deserializer:
public GeneratedClassFromXSD GetObjectFromXML()
{
var settings = new XmlReaderSettings();
var obj = new GeneratedClassFromXSD();
var reader = XmlReader.Create(urlToService, settings);
var serializer = new System.Xml.Serialization.XmlSerializer(typeof(GeneratedClassFromXSD));
obj = (GeneratedClassFromXSD)serializer.Deserialize(reader);
reader.Close();
return obj;
}
Deserialize any object, as long as the type T is marked Serializable:
function T Deserialize<T>(string serializedResults)
{
var serializer = new XmlSerializer(typeof(T));
using (var stringReader = new StringReader(serializedResults))
return (T)serializer.Deserialize(stringReader);
}
Well, you'd have to have classes in your assembly that match, roughly, the XML (property called Private, a collection property called ToDo, etc).
The problem is that the XML has elements that are invalid for class names. So you'd have to implement IXmlSerializable in these classes to control how they are serialized to and from XML. You might be able to get away with using some of the xml serialization specific attributes as well, but that depends on your xml's schema.
That's a step above using reflection, but it might not be exactly what you're hoping for.
Checkout http://xsd2code.codeplex.com/
Xsd2Code is a CSharp or Visual Basic Business Entity class Generator from XSD schema.
There are a couple different options.
Visual Studio includes a command line program called xsd.exe. You use that program to create a schema document, and use the program again on the schema document to creates classes you can use with system.xml.serialization.xmlserializer
You might just be able to call Dataset.ReadXml() on it.
i had the same questions few years back that how abt mapping xml to C# classes or creating C# classes which are mapped to our XMLs, jst like we do in entity Framework (we map tables to C# classes). I created a framework finally, which can create C# classes out of your XML and these classes can be used to read/write your xml. Have a look

Categories