tags:

views:

170

answers:

9

though we can query on the xml file from c# (.net). why need of .xsd file ? I know it is metadata file of particular xml file. we can specify the relationships in xsd. but what its functioning then ?

Sure: See, I have the xml:

<?xml version="1.0" encoding="utf-8" ?>
<Root>
  Customers->
    ->Customer 
        ->CustomerID="GREAL">->
      <CompanyName>Great Lakes Food Market</CompanyName>
      <ContactName>Howard Snyder</ContactName>
      <ContactTitle>Marketing Manager</ContactTitle>
      <Phone>(503) 555-7555</Phone>
      <FullAddress>
        <Address>2732 Baker Blvd.</Address>
        <City>Eugene</City>
        <Region>OR</Region>
        <PostalCode>97403</PostalCode>
        <Country>USA</Country>
      </FullAddress>
    <-</Customer>

 <- </Customers>



<Orders>
    <Order>
      <CustomerID>GREAL</CustomerID>
      <EmployeeID>6</EmployeeID>
      <OrderDate>1997-05-06T00:00:00</OrderDate>
      <RequiredDate>1997-05-20T00:00:00</RequiredDate>
      <ShipInfo ShippedDate="1997-05-09T00:00:00">
        <ShipVia>2</ShipVia>
        <Freight>3.35</Freight>
        <ShipName>Great Lakes Food Market</ShipName>
        <ShipAddress>2732 Baker Blvd.</ShipAddress>
        <ShipCity>Eugene</ShipCity>
        <ShipRegion>OR</ShipRegion>
        <ShipPostalCode>97403</ShipPostalCode>
        <ShipCountry>USA</ShipCountry>
      </ShipInfo>
    </Order>


    <Order>
      <CustomerID>GREAL</CustomerID>
      <EmployeeID>8</EmployeeID>
      <OrderDate>1997-07-04T00:00:00</OrderDate>
      <RequiredDate>1997-08-01T00:00:00</RequiredDate>
      <ShipInfo ShippedDate="1997-07-14T00:00:00">
        <ShipVia>2</ShipVia>
        <Freight>4.42</Freight>
        <ShipName>Great Lakes Food Market</ShipName>
        <ShipAddress>2732 Baker Blvd.</ShipAddress>
        <ShipCity>Eugene</ShipCity>
        <ShipRegion>OR</ShipRegion>
        <ShipPostalCode>97403</ShipPostalCode>
        <ShipCountry>USA</ShipCountry>
      </ShipInfo>
    </Order>

  </Orders>
</Root>

I want to get data from the ORDERS elements according to provided CustomeId.

Also questions is: What is the purpose of giving the relationships in xsd.

+3  A: 

XSD files are used to validate XML - that is conforms to a certain format.

In that respect they are similar to DTDs that existed before them.

The main difference between XSD and DTD is that XSD is written in XML and is considered easier to read and understand.

Oded
thanks, so can i query on xsd files from c3.net using Linq for get the data from xml file? How?
Lalit
@Lalit - Linq has nothing to do with it.
Oded
Another important difference is that you can't do lots of stuff easily done in XSD in a DTD.
mort
So for getting data, I can use only the xml file right? I have no need to generate its schema file. (I don't want to validate it for now) I just want to get data of xml that have various records dependent on IDs maps in each element.
Lalit
@Lalit - that's right.
Oded
@mort - there are also things you can do with DTDs that cannot be done in XSD.
Oded
@Oded - yes, but IMHO a lot of basic stuff is a real pain in DTD
mort
Ok thanks all, So waht is main purpose of setting the relationships in schema file then? why its need.
Lalit
@mort - never said that wasn't the case... that's why you don't see DTDs as much anymore :)
Oded
@Lalit - If you _need_ to make sure that the relationships are correct, you can use an XSD to validate the XML, to make sure that it has the correct relationships. If you don't care about such things, you don't need XSD.
Oded
Ok that's means though I what to get data from xml file then no need to set relationships ? hmmmmmm... thanks
Lalit
@Lalit - if you don't care about the relationships as such and don't need to check that they conform to some sort of rule (codified in an XSD), then don't use an XSD and just query the XML directly.
Oded
+1  A: 

The xsd file is the schema of the xml file - it defines which elements may occur and their restrictions (like amount, order, boundaries, relationships,...)

mort
A: 

Read this post and you'll get the idea.

thelost
But still not satisfied. Can we use xsd for getting the data from xml ? using c#.
Lalit
No, you'll need a xml parser for that, e.g. http://support.microsoft.com/kb/307548
mort
I think LINQ is more powerful than using parsers
Lalit
+1  A: 

An XSD file is an XML Schema Definition and it is used to provide a standard method of checking that a given XML document conforms to what you expect.

ar
+1  A: 

You mention C# in your question so it may help to think of as XSD as serving a similar role to a C# interface.

It defines what the XML should 'look like' in a similar way that an interface defines what a class should implement.

Garry
please clear, I am not getting.
Lalit
If you take a class and an interface it is supposed to implement, you can tell if the class is correct. In the same way, if you take an xml file and it's xsd, you can tell if the xml is correct.
Garry
+1  A: 

An .xsd file is called an XML schema. Via an XML schema, we may require a certain structure in a given XML - which elements in which order, how many times, with which attributes, how they are nested, etc. If we have a schema for our XML input, we can verify that it contains the data we need it to contain, and nothing else, with a few lines invoking a schema validator.

delnan
+3  A: 

Without XML Schema (XSD file) a XML file is a relatively free set of elements and attributes. XSD file define which elements and attributes are permitted and in which order.

In general XML is a metalanguage. XSD define the language which you choosed. For example, you can use XSD which define XHTML 1.0, then your XML file permitted be only XHTML 1.0 and nothing more.

Oleg
+1  A: 

Also questions is: What is the purpose of giving the relationships in xsd.

Suppose you want to generate some XML for an external party's tool, or similar - how would you know what structure it is allowed to follow to be used correctly for their tool? you write to a schema. Likewise if you want other people to use your tool, you would write a schema for them to follow. It may also be useful for validating your own XML.

Robert
+1  A: 

An XSD is a formal contract that specifies how an XML document can be formed. It is often used to validate an XML document, or to generate code from.

troelskn