0

I'm working with C#, and I'm attempting to Deserialize an XML file into my application. This is a threaded process that pulls XML files from multiple directories, where each directory may have a specific schema based on our customers' needs.

What I'm trying to do is using a schema for a specific directory, and the deserializer I need to Load the data from the XML file into my object in my application. However, I need the deserializer to take the default values from the Schema if the element is not specified in the XML file.

I've found that there is a way to specify the schema location for a class using the [XmlSchemaProviderAttribute("GetSchemaFile")] attribute, and a static method (ref http://msdn.microsoft.com/en-us/magazine/cc300797.aspx). This however will not work for my current situation since I have multiple schemas specific to different files.

Does anyone know of a way to do this? Or should I attempt to find an alternate way of dealing with my need for defaults?

Benji Vesterby
  • 574
  • 5
  • 21

2 Answers2

1

Each XML file should specify the schema that it uses. This is typically done in the root element.

In the example below, the schema was located in the same directory as the XML file. Obviously, this would change, depending on where the schema is located, relative to the XML file. However, the point is that you use the XML file to reference the schema. Then, any default values will be automatically applied by your XML parser.

<node name="/org/freedesktop/sample_object" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:noNamespaceSchemaLocation="introspect.xsd">

Here's a link with a brief overview of how to reference schema.

David
  • 6,462
  • 2
  • 25
  • 22
  • The XML files that I am currently using specify the Namespace of the Schema file, however the XML files are being sent from our customers who have no knowledge of where the Schema for their XML is actually located. The application tracks the location of the schema file for each of our customers. Is there a way to use that information programmatically and interface it with the Serializer? – Benji Vesterby Aug 08 '12 at 21:41
  • Could you serve the schema via HTML, then share the list of URLs with your customer so that they could reference them? For example: `xsi:schemaLocation="http://www.example.org example.xsd` – David Aug 08 '12 at 21:57
  • The problem I run into with going that direction is that some of our customers are unwilling to change their Export tools to use that method. The changes I'm doing need to be as invisible to our customers as possible. – Benji Vesterby Aug 08 '12 at 22:02
  • Actually, I just had a brain flash. I think that most XML validating parsers provide a means to override the schema location specified in the XML (e.g. `DocumentBuilderFactory` in Java). So, checkout the API for your parser class for a way to specify the schema location. – David Aug 08 '12 at 22:16
  • This similar [post](http://stackoverflow.com/questions/9094662/validate-an-xml-against-an-xsd-in-java-getting-a-hold-of-the-schemalocation) might be helpful. – David Aug 08 '12 at 22:24
0

So I just had a chance to test your suggestion @David of using the noNamespaceSchemaLocation, and the XmlSerializer doesn't seem to pull any information from the schema listed there for defaults. I've been looking all day to find a way to make this work, hence the reason I posted.

* EDIT *

After having looked through many different sites I have found that using the C# XML Serializer Deserialization method to pull defaults from the schema is not possible if the Schema has to be dynamic, meaning that the object being deserialized can have different defaults in different Schemas.

I have a solution that will work in my current situation to resolve this issue for our software to deal with defaults.

Thanks @David for the suggestions. They are much appreciated.

Benji Vesterby
  • 574
  • 5
  • 21
  • The solution I used it unique to my situation. What I ended up doing is using a settings file for each customer which contained the default information (ex => ClassName, PropertyName, DefaultValue), and used reflection to apply it after deserializing the XML into the application. – Benji Vesterby Aug 10 '12 at 20:57