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.
Related
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'.
I want to create an nested XML file for my current application.
The inputs data for the XML are taken from the following form.Problem that i am facing is for the block.
Inside this block i have to take data from datagridview and have to save the data to the XML:
Table Name Field FieldType
Table1 f1 Search
Table1 f2 Update
Table2 f1 Search
Table2 f2 Update
The XML structure is given here.
<?xml version="1.0"?>
-<database>
-<databasedtls>
<databasename/>
<path/>
<port/>
<user/>
<password/>
</databasedtls>
-<tables>
-<table>
<tablename>TABLE1</tablename>
-<FIELDS>
-<Search>
<field/>
<field/>
</Search>
-<Update>
<field/>
<field/>
</Update>
</FIELDS>
</table>
-<table>
<tablename>TABLE2</tablename>
-<FIELDS>
-<Search>
<field/>
<field/>
</Search>
-<Update>
<field/>
<field/>
</Update>
</FIELDS>
</table>
</tables>
</database>
Please help me regarding this.....
You could do this using the .NET XMLSerializer. It allows you to serialize objects to XML. here are two good links
This gives good basic examples of how to use the XML Serializer
http://msdn.microsoft.com/en-us/library/58a18dwa(v=vs.110).aspx
This introduces XML Attributes, you can use these on your own classes to control what the output XML look like when you serialize a class instance
http://msdn.microsoft.com/en-us/library/2baksw0z(v=vs.110).aspx
This would require you to build a few classes, my recommendation based on your structure
Classes:
database
databasedtls
table
Field(abstract)
SearchField(extends Field)
UpdateField(extends Field)
these classes should have the appropriate properties based on the xml structure for example database should have a databasedtls property and a list of tables.
If the above is too complex, or is too much of a time investment, you could do string manipulation or modify a XMLDocument object directly, but i recommend against that if you can.
That sort of code can prove difficult to maintain, and is more prone to supporting bad OOP practices, but they are also options so you know.
I have been searching and trying in many forms to pass an XML Response to a simple DataTable in the same way Excel does.
The XML is like:
<?xml version="1.0" standalone="no"?>
<SOAP-ENV:Envelope xmlns:SOAPSDK1="http://www.w3.org/2001/XMLSchema" xmlns:SOAPSDK2="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAPSDK3="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAPSDK4:Function xmlns:SOAPSDK4="http://www.externalwebservice.com/message/">
<Lists>
<ListCode>12345</ListCode>
<ListGroups>
<ListGroup>
<CodeGroup>ASDF</CodeGroup>
<DescriptionGroup>Example</DescriptionGroup>
</ListGroup>
</ListGroups>
<List>
<CodeList>ABC</CodeList>
<DescriptionList>Example List</DescriptionList>
<ListCategories>
<ListCategory>Type1</ListCategory>
<ListCategory>Type2</ListCategory>
</ListCategories>
<ListKinds>
<Kind>
<KindType>A</KindType>
<KindTarget>1</KindTarget>
<KindAttributes>
<KindAttribute1>A</KindAttribute1>
<KindAttribute2>B</KindAttribute2>
</KindAttributes>
</Kind>
</ListKinds>
</List>
</Lists>
</SOAPSDK4:Function>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
What Excel does could help me in what I need and that is to fill 4 tables with this data and its relations.
I have tried using DataSet.ReadXML but the problem with it is that for some reason this method generates 14 tables with the data being of no use because there is no relation at all.
Later I tried with DataTable.ReadXML but it gave me the problem "DataTable does not support schema inference from Xml." then tried to use DataTable.ReadXmlSchema(XML) and it gave an error with no root document, then I created an XSD with the command prompt and the two generated gave an error, finally I used Visual Studio to generate the XSD, it generated 4 XSD with no errors but no columns where created and no data was added with none of them.
I am out of ideas now. Any suggestions? Maybe not a DataTable with no normalized data (what Excel does) but another approach going directly with the XML?
Thank you.
My suggestion would be Linq.XML technique in C# 3. LINQ to XML provides an in-memory XML programming interface that leverages the .NET Language-Integrated Query (LINQ) Framework. LINQ to XML uses the latest .NET Framework language capabilities and is comparable to an updated, redesigned Document Object Model (DOM) XML programming interface.
There are few interesting quick relevant examples are available at http://www.hookedonlinq.com/LINQtoXML5MinuteOverview.ashx
I want to know the efficient way to store the configuration xml to be used in the program. I want to load the following config xml into memory as soon as I start the program and then use the properties where needed.
XML:
<ViewModelConfiguration>
<FICallSchedule>
<Model>
<Details>
<DataSource>
<Dataset se-datafilter="callschedule" dv-datamanipulationrequired="false" dv-filtercondition="" dv-sortcolumn="" dv-gettopNrows="" />
<XmlData></XmlData>
</DataSource>
<ComputePercentage isactive="true" dataorientation="horizontal">
<column source="value1" destination="value3" datafilter="" />
<column source="value2" destination="value4" datafilter="" />
</ComputePercentage>
</Details>
</Model>
</FICallSchedule>
</ViewModelConfiguration>
Currently I am reading the main tags like <Dataset> and load all the attributes of it in dictionary object. And then use that dictionary with key in my code. Similarly I do it for ComputePercentage and so on and so forth.
I was wondering if I can load the complete XML into some object and access each node or attribute something like: Model.Details.Dataset.DataSource.se-datafilter or Model.Details.ComputePercentage which will return a collection of columns. I am not sure if this makes any sense otherwise I would stick to dictionary objects only.
If the configuration file has some predefined schema - it stands to reason that it does - you can use the xsd tool to generate a class based on that schema. There is also a free xsd2code library that does similar thing.
But you have to define your Model class somehow; either by autogenerating it with the aid of these tools, or by implementing it "manually".
Take use of configSection and configElement, that way elements and attributes will be loaded as object, and this configuration can be stored into application config file.
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