XML Data and Schemas - c#

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'.

Related

Cloning elements and changing its order by comparing xml's

I have a source xml which needs to be compared against xml template(another xml file).
The following needs to be implemented,
The order of the source xml needs to be transformed as per the order in the template xml.
If the elements in the template xml are not found in the source xml then it should be added.
Basically what should I need to do this, compare 2 xml's and need to change one xml based on other.
I have an idea of creating XDocument with 2 xml's and by accessing xpath of source xml from template xml, Rearrange the elements and add elements if not found.
Please advise whether this is a better approach, or anything can be added into this?

converting xml data type in sql server to mongodb

Just wondering if there's a way to facilitate a procedure, where I have an xml document, no attributes, it's purely nodal, that is saved within a column of a table in sql server, then traverse through this column, and convert it to json, then take that final outcome, and dump it into mongodb?
It does seem like a tall order, but I feel like there must be a way where I can take each xml data object stored in this column, convert to json in c# using some library (I seen some out there that exists), but I really don't want the hassle of dumping them into a file. The process of the conversion and dumping into mongodb seems like it'd take a very very long time to parse and insert. So I was wondering if there's an alternative route that'd make my long journey less troublesome.
[EDIT: adding example]
Here's an example of what could be in a column as xml data in a sql server column:
<Person>
<FirstName><![CDATA[John]]></FirstName>
<LastName><![CDATA[Doe]]></LastName>
<Person>
Or sometimes, I could have a list of things:
<Cities>
<City>
<Name>Cleveland</Name>
<State>Ohio</Name>
</City>
<City>
<Name>San Francisco</Name>
<State>California</State>
</City>
</Cities>
Now, is there a way to take these examples, convert them to json, in a bulk way for each column containing xml, and then dump them into mongodb? Do I have to write out this process in c#? What's a good way of completing this task efficiently?

XML attribute to indicate overwrite vs. preserve existing value

I could use a little guidance on working with XML.
I'm working on a several XML interop specifications which will be used to create or update data in our system. I'd like to provide a way for my customers to specify which elements they want to maintain via automation using the XML spec vs. manually updating the records after the fact. The quick back story to this is that our customers have upstream applications that can supply some but typically not all of the data required by our application so they will typically be sending us partial information for a record and then maintaining the rest in our app.
So assuming a simple XML:
<Data>
<Element1 />
<Element2 />
<Element3 />
</Data>
Customer A may supply element 1 and 2 but not element 3 so they would want to configure 3 to persist values between updates but for 1 and 2 to always overwrite with the new value.
Customer B may supply element 1, 2 and 3 and want to configure to always overwrite all 3 elements.
So I guess what I'm really looking for with Customer A is something like this:
<Data>
<Element1>data1</Element1>
<Element2>data2</Element2>
<Element3 overwriteExistingData="false"></Element3>
</Data>
I understand XML to a degree but have found it very easy to create an XML schema by simply creating C# class, doing some decorating with attributes if necessary and then generating a schema with xsd.exe and calling it a day.
Things that I'm unsure of are
- how to distinguish between a null value and something that the customer didn't have a value for
- the best way to handle these partial data situations where I'm not going to get a complete replacement record for every update.
I'm sure I am missing the boat to some degree but I really like the simplicity of having a class definition and telling the customer to give me XML that allows me to just type one line of code to deserialize the XML into my C# object.
Any guidance would be much appreciated

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.

When should I use xml schemas(.xsd) when reading xml?

I have a XML file which holds configuration data for data sources, and the related queries held in 'dataseries' elements.
As I don't really need domain objects made up from the XML, rather just settings read and used to configure connections etc. I was wondering if there is any advantage in using the XML schema I have defined?
I am using LINQ to XML for reading my XML and initially thought it would be a good idea to use strongly typed XML.
Should I be using the .xsd or is it overkill?
A mock XML file:
<?xml version="1.0" encoding="utf-8" ?>
<datasource name=" Datasource" cache="true">
<database>
<connection>
<provider-name>sqlServer6.0</provider-name>
<source name="E5"
connectionString=""/>
</connection>
<update-interval>30</update-interval>
<minimum-update-interval>2</minimum-update-interval>
</database>
<dataseries name="" identifier="e5">
<graph-type></graph-type>
<query>
SELECT Period, Price
FROM PriceUS
WHERE Date = #date
</query>
</dataseries>
<dataseries name="" identifier="e52">
<graph-type></graph-type>
<query>
SELECT Period, Price
FROM PriceUS
WHERE Date = #date
</query>
</dataseries>
</datasource>
There are two levels of "correct" XML documents: well-formed and valid.
Well-formed means it conforms to XML spec, and valid means that it conforms to your schema. If and when you are accepting an XML document from a total stranger, it's usually a good idea to check the validity of the document before moving forward.
As you mentioned, XML schema could also be used to generate XML databinding entities. When you publish a service or a schema to the world or your client, the schema document could be used as a spec. The world or your client can then use the XSD file to validate or databind to XML documents that you exchange.
Technically, there are two formal schema types in XML. There's the original schema syntax, called a Document Type Definition (DTD), which is a holdover from the ancient SGML days. And then there is the W3C standard, XSD, which is more compatible with modern data.
The only reason I mention this is that you might receive an XML file that is described using a DTD and you might need to know how to deal with it.
But please, friends don't let friends create DTDs for new applications.
A schema is good when you want to validate the XML, for example, ensure that certain elements are present or have a limited set of values. A schema is also good if you will be doing a lot of work with the XML and it would be easier to convert the XML into C# objects -- in this case you can use the xsd.exe code generator to generate C# objects that can be marshaled and unmarshaled from the XML.

Categories