XSD File and Export - c#

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

Related

How to convert xml word table to html using c#

I have a word document (.xml file) and I need to convert the tables to html. Is there some kind of existing tool for c#? The way I get the xml is from the document is:
Table table = element.Descendants<Table>().First();
string ttt = table.InnerXml;
The Open XML PowerTools have the functionality to transform Word documents into HTML. Note, though, that they are not using the strongly-typed classes of the Open XML SDK but rather the Linq to XML (e.g., XElement) classes.
BTW, you would never use the inner XML to transform an element but rather the outer XML, which includes the w:tbl element in your case.

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

Serialize object to XML based on existing XSD schema

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

Saving an XML file using Fileupload

How can I save the content of an xml file to a database( in field which has an XML type)
should I read the content of file with i.e:
FileUpload1.FileContent;
and then send it as a parameter to save in Database? Is it correct?
You have to first save it to the Server hard disk and then get the InnerXML of that to a string variable and then save it to the database.
Assuming you saved the file to some folder in your disk, you can use the below method (using LINQtoXML) to read the content
XElement elm = XElement.Load(Server.MapPath(#"../YourUploadFolder/yourXMl.xml"));
if(elm!=null)
{
var reader = elm.CreateReader();
reader.MoveToContent();
string xmlContent = reader.ReadInnerXml(); xmlContent
// Now save to the database
}
You should use the the XmlReader and XmlTextReader classes to load the XML file into memory. They are defined in the System.XML namespace. You could also use the XDocument class defined in the System.Xml.Linq namespace. For more information please look here :
http://www.c-sharpcorner.com/uploadfile/mahesh/readingxmlfile11142005002137am/readingxmlfile.aspx
http://support.microsoft.com/kb/307548
var reader = new XmlTextReader("C:\\temp\\xmltest.xml");
You then store the XML content as XML in the DB if possible (depending on the DB system you use) or as varchar. Would be better to store them as XML though since you may then assure that its is well-formatted and validated against a certain schema for example !
You can basically fill columns of type XML from an XML literal string, so you can easily just use a normal INSERT statement and fill the XML contents into that field. For this, you have to read the XML file.
You can use the System.Xml.Linq namespace and use XDocument.Load(#"YourXmlFile.xml"); or any standard method to read the XML file as described here - http://support.microsoft.com/kb/307548

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