how to set XmlAttribute as IsRequired? - c#

Can we have a required XmlAttribute that does not allow null value?
I want to have something like IsRequired = true in XmlAttribute. Can it be done? I know that there is a 'use="required"' for XmlAttribute, but we can't set its value, can we? Is there any trick to serve this purpose?

I'm still a little confused by what you're trying to do, so i'm assuming that you wish to validate your input xml before attempting to use it in your webservice. In your case your XML needs to have a specific attribute.
To do something like that I typically create an XSD against the XML that should be received and use that to validate it against the XML. The XSD will contain the details that the XML must contain a certain node / attribute. You can use visual studio XSD editor to configure these items.
This topic should help you with the validation code once you're XSD is created:
Validating an XML against referenced XSD in C#

Related

XSD2CODE - Remove xsi:type tag from XML

I'm serializing an object automatically created by Xsd2Code, the resulting XML has xsi:type tags under each of the xml values:
<shape xsi:type="xsd:int">1</shape>
I need to remove these and if doing that manually i would normally just change the namespace but here i want to do this with the Xsd2Code generator, does anyone know of which config needs to be changed to achieve this?

Avoid serialize propertie/field that is not set?

I have generated a WCF proxy from a WSDL file and are having problems with a property/field.
The propertie is of a complex type(class) and is decorated with the following attribute in the generated code.
XmlElementAttribute(IsNullable=true)
When the propertie is not set to an object the serializer will still generate it in the XML like this
<OpDDoctorInfo xsi:nil="true" />
I need it to not be generated at all.
I really dont like to change in a generated proxy file but I dont seem to have any other choise?
From this link I can see that if there is a default value and the value that is set is the same then the default behavior will be to exclude it from the xml so why have it been generetad with a attribute like this?
Edit: some thinks that this is a dubblicate of this :
What is the correct way to represent null XML elements?
It is not, that questions asks what the correct way is to handle null. Im asking how to remove the propertie/field from the generated XML while using a generated proxy class. Maybe I have to change in the proxy generated code or is there a better way?

XMLSerialization and XML Schema validation

Is it possible to use the XML Schema validation and XMLSerializer together?
My project currently uses XMLSerializer. To validate the schema, we are programatically checking the values like:
if(String.IsNullOrEmpty(person.Name))
throw new Exception();
Thanks!
You can add some additional checking to the xsd, by using the element restriction element. Each type have some facets that you can apply to the type/element - ranging from simple min/max length to regular expressions.
You can even take it one step further and use the appinfo xsd element, where you can add custom specification for etc. validation checking. This step however require you to parse each xml node individually, as the normal Xml Schema Validation don't trigger appinfo functionality.
As a last resort you can even have a look at Schematron, which is a formalized way to add quite complex validation to your xsd, but it is in itself a bit complex, and in many situations overkill.
I would suggest using the XmlValidatingReader for validatiing the schema. Please see http://www.codeproject.com/KB/XML/Serialization.aspx for an example...
Actually you can just provide an XSD within the XML. When you read it using the XMLSerializer, it will throw exceptions if the XML is not matching the XSD.

XmlSerializer.Deserialize - ignore unnecessary elements?

I've got an XSD schema which I've generated a class for using xsd.exe, and I'm trying to use XmlSerializer.Deserialize to create an instance of that class from an XML file that is supposed to conform to the XSD schema. Unfortunately the XML file has some extra elements that the schema is not expecting, which causes a System.InvalidOperationException to be thrown from Deserialize.
I've tried adding <xs:any> elements to my schema but this doesn't seem to make any difference.
My question is: is there any way to get XmlSerializer.Deserialize to ignore these extra elements?
I usually add extra properties or fields to all entity classes to pick up extra elements and attributes, looking something like the code below:
[XmlAnyAttribute]
public XmlAttribute[] AnyAttributes;
[XmlAnyElement]
public XmlElement[] AnyElements;
Depending on the complexity of your generated code, you may not find hand-inserting this code on every entity appealing. Perhaps only-slightly-less-tedious is defining these attributes in a base class and ensuring all entities inherit the base.
To give fair attribution, I was first introduced to this pattern when reading the source code for DasBlog.
I don't think there is an option to do this. You either have to fix the schema or manually modify the code generated by xsd.exe to allow the XML to be deserialized. You can also try to open the XML document + schema in Visual Studio or any other XML editor with schema support to either fix the schema or the XML document.

Is it possible to set a default value when deserializing xml in C# (.NET 3.5)?

I've got a little problem that's slightly frustrating. Is it possible to set a default value when deserializing xml in C# (.NET 3.5)? Basically I'm trying to deserialize some xml that is not under my control and one element looks like this:
<assignee-id type="integer">38628</assignee-id>
it can also look like this:
<assignee-id type="integer" nil="true"></assignee-id>
Now, in my class I have the following property that should receive the data:
[XmlElementAttribute("assignee-id")]
public int AssigneeId { get; set; }
This works fine for the first xml element example, but the second fails. I've tried changing the property type to be int? but this doesn't help. I'll need to serialize it back to that same xml format at some point too, but I'm trying to use the built in serialization support without having to resort to rolling my own.
Does anyone have experience with this kind of problem?
It looks like your source XML is using xsi:type and xsi:nil, but not prefixing them with a namespace.
What you could do is process these with XSLT to turn this:
<assignees>
<assignee>
<assignee-id type="integer">123456</assignee-id>
</assignee>
<assignee>
<assignee-id type="integer" nil="true"></assignee-id>
</assignee>
</assignees>
into this:
<assignees xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<assignee>
<assignee-id xsi:type="integer">123456</assignee-id>
</assignee>
<assignee>
<assignee-id xsi:type="integer" xsi:nil="true" />
</assignee>
</assignees>
This would then be handled correctly by the XmlSerializer without needing any custom code. The XSLT for this is rather trivial, and a fun exercise. Start with one of the many "copy" XSLT samples and simply add a template for the "type" and "nil" attributes to ouput a namespaced attribute.
If you prefer you could load your XML document into memory and change the attributes but this is not a good idea as the XSLT engine is tuned for performance and can process quite large files without loading them entirely into memory.
You might want to take a look at the OnDeserializedAttribute,OnSerializingAttribute, OnSerializedAttribute, and OnDeserializingAttribute to add custom logic to the serialization process
XmlSerializer uses xsi:nil - so I expect you'd need to do custom IXmlSerializable serialization for this. Sorry.

Categories

Resources