Serialize object to XML based on existing XSD schema - c#

I have to create an XML document that would be based on an certain XML Schema Document. Since my data is a DataSet, I need to find the best way to start off.
I have couple of different ideas how to start:
manually create nodes, elements, attributes that would match XSD
transform DataSet into a class that would match the schema document and serialize it
something else?
Is this a right way to get a XML output from DataSet to match XSD schema?

May be you should give XMLBeans a try... It's a diverse framework for playing around with compiled XSD schemas. Compiled in this context means, you create JAVA classes from your XSD-files.
Compilation example (as can be seen here) scomp -out purchaseorder.jar purchaseorder.xsd
With this jar in your classpath you could create new a priori valid instances of your schema with something like:
public PurchaseOrderDocument createPO() {
PurchaseOrderDocument newPODoc = PurchaseOrderDocument.Factory.newInstance();
PurchaseOrder newPO = newPODoc.addNewPurchaseOrder();
Customer newCustomer = newPO.addNewCustomer();
newCustomer.setName("Doris Kravitz");
newCustomer.setAddress("Bellflower, CA");
return newPODoc;
}
You can find the whole example at: XMLBeans Tutorial under the heading "Creating New XML Instances from Schema".

Related

XML Data and Schemas

Ok, here is specific case scenario:
My application is going to receive some XML inputs. Then the application needs to render that XML input, as well as do some calculations after parsing data from that XML input.
The deal is, that the application is data agnostic. It's code cannot know details about XML data and format during design-time. So am making it the responsibility of calling client tool to send a schema associated with the XML data. Based on that schema, application will parse and understand XML data it will receive.
So, questions:
Can XML Schema specify any custom attributes that I may decide my application will need to parse data?
Will it be ok if corresponding node in XML data will not specify those attributes themselves?
While navigating in XML data, node by node, how can I using C# load corresponding attributes and values from XML schema?
Basically, I'll need such custom attributes in schema for various nodes - showInTable, isPrimary, graphable etc etc
Thanks for help.
The way around this I would say is to have a some fixed part of the schema, for data that will be there - even if it is nullable.
Then after that, get the XML to use some sort of <metadata> tags to allow you to capture any additional information. Like
<Customer>
<Name>Joe Bloggs</Name>
<Age>65</Age>
<Metadata key="Criminal History">Grand Theft Auto</Metadata>
<Metadata key="Favourite Colour">Blue</Metadata>
</Customer>
Metadata can be shared (if defined up front), with a minOccurs='0', maxOccurs='unbounded'.

XSD File and Export

I am new to XSD. I want to know how XSD files are used to export data to XML.
I assume that same XSD file can be used while imorting the XML file to check if the schema of the XML file is as per the schema of XSD file. I hope this assumption is correct.
Thanks,
Ram
Some corrections in your statement:
XSD file doesn't export the data .. it is used to validate the data.
XSD file can be used to validate XML while importing it .. so as to check if XML file is as per the SCHEMA file. XSD-XML Schema Definition
ps: Normal convention is .. XSD is used on imported data than
exporting ones.. because you know what you are sending .. but you
should be sure enough to accept the valid data that is sent to you by
other system.. (no harm in validating out-going data though)
XML stands for EXtensible Markup Language
XML is a markup language
much like HTML (but not HTML)
XML was designed to carry data, (not to
display data)
No tags or attributes are predefined. What you define
is your data :)
The purpose of an XML Schema is to define rule-sets for an XML document, just like a DTD.
(its much more advanced than DTD.)
Refer this link [click_here] to know capabilities and limits of XSD
XSD files are used to check the schema of the XML:
XmlReaderSettings xmlSettings = new XmlReaderSettings();
xmlSettings.ValidationType = ValidationType.Schema;
xmlSettings.Schemas.Add("http://www.example.com/SchemaName",
"http://intranet/xml/schemadatei.xsd");
XmlReader xmlReader = XmlReader.Create(this.dateiname, xmlSettings);
while (xmlReader.Read())
{ }
And you can generate from XSD a class as you can see here: http://msdn.microsoft.com/de-de/library/x6c1kb0s%28v=vs.80%29.aspx

XML from DataTable using Linq

This code
XmlDataDocument xmlDataDocument = new XmlDataDocument(ds);
does not work for me, because the node names are derived from the columns' encoded ColumnName property and will look like "last_x20_name", for instance. This I cannot use in the resulting Excel spreadsheet. In order to treat the column names to make them something more friendly, I need to generate the XML myself.
I like LINQ to XML, and one of the responses to this question contained the following snippets:
XDocument doc = new XDocument(new XDeclaration("1.0","UTF-8","yes"),
new XElement("products", from p in collection
select new XElement("product",
new XAttribute("guid", p.ProductId),
new XAttribute("title", p.Title),
new XAttribute("version", p.Version))));
The entire goal is to dynamically derive the column names from the dataset, so hardcoding them is not an option. Can this be done with Linq and without making the code much longer?
It ought to be possible.
In order to use your Dataset as a source you need Linq-to-Dataset.
Then you would need a nested query
// untested
var data = new XElement("products",
from row in ds.Table["ProductsTable"].Rows.AsEnumerable()
select new XElement("product",
from column in ds.Table["ProductsTable"].Columns // not sure about this
select new XElement(colum.Fieldname, rows[colum.Fieldname])
) );
I appreciate the answers, but I had to abandon this approach altogether. I did manage to produce the XML that I wanted (albeit not with Linq), but of course there is a reason why the default implementation of the XmlDataDocument constructor uses the EncodedColumnName - namely that special characters are not allowed in element names in XML. But since I wanted to use the XML to convert what used to be a simple CSV file to the XML Spreadsheet format using XSLT (customer complains about losing leading 0's in ZIP codes etc when loading the original CSV into Excel), I had to look into ways that preserve the data in Excel.
But the ultimate goal of this is to produce a CSV file for upload to the payroll processor, and they mandate the column names to be something that is not XML-compliant (e.g. "File #"). The data is reviewed by humans before the upload, and they use Excel.
I resorted to hard-coding the column names in the XSLT after all.

Map multiple xml feeds to one object

Using C# I want to be able to map a number of xml feeds to one custom object. Each xml feed has the same kind of data but has its own naming convention.
Ideally i would like to store for each xml feed its own mapping and apply that automatically when copying the xml data to my object. I would like to do this as the system may grow to hundreds of feeds so just being able to store the mappings would make it easier to maintain than writing code for each feed.
So for example, my object consists of
ID, Name
And xml feed one is
Code, ProductName
xml feed two is
UniqueID, FullName
so the mappings would be
ID -> Code
Name -> ProductName
and
ID -> UniqueID
Name -> FullName
What would be the best way of achieving this?
I would create a configsection in your config file. You could then have a node for each feed. Then have nodes within that have the mapping information. The nodes in your feed node would match the properties in your c# object and the node value would be the node name in your xml file. You could also even add the full xpath path if it was more complicated.
<feed url="">
<id>Code</id>
<Name>ProductName</Name>
</feed>
Then in your app you could load the feed. Then search for the node in your config file to get how to map the fields to your C# object from fields in your xml file.
Just one approach that would make it easy to configure and grow without changing the application unless your c# object changes.

XML tag name being overwritten with a type defined

We are communicating with a 3rd party service using via an XML file based on standards that this 3rd party developed. They give us an XML template for each "transaction" and we read it into a DataSet using System.Data.DataSet.ReadXML, set the proper values in the DataSet, and then write the XML back using System.Data.DataSet.WriteXML. This process has worked for several different files. However, I am now adding an additional file which requires that an integer data type be set on one of the fields. Here is a scaled down version:
<EngineDocList>
<DocVersion>1.0</DocVersion>
<EngineDoc>
<MyData>
<FieldA></FieldA>
<FieldB></FieldB>
<ClientID DataType="S32"></ClientID>
</MyData>
</EngineDoc>
</EngineDocList>
When I look at the DataSet created by my call to ReadXML to this file, the MyData table has columns of FieldA, FieldB, and MyData_ID. If I then set the value of MyData_ID and then make the call to WriteXML, the export XML has no value for ClientID. Once again, if I take a way the DataType, then I do have a ClientID column, I can set it properly, and the exported XML has the proper value. However, the third party requires that this data type be defined.
Any thoughts on why the ReadXML would be renaming this tag or how I could otherwise get this process to work? Alternatively, I could revamp the way we are reading and writing the XML, but would obviously rather not go down this path although these suggestions would also be welcome. Thanks.
I would not do this with a DataSet. It has a specific focus on simulating a relational model. Much XML will not follow that model. When the DataSet sees things that don't match it's idea of the world, it either ignores them or changes them. In neither case is it a good thing.
I'd use an XmlDocument for this.

Categories