Link to home
Start Free TrialLog in
Avatar of pprasadravi
pprasadravi

asked on

Asp.Net,Xml and Xsd

Hi

Here is my xml structure, xsd and asp.net code.
Problem :

It is displaying all column type is string

XML Structure (File Name : XMLFile1.xml):

<main>
             <SummaryInformation >
                    <PickupDate>"10/29/1845"</PickupDate>
                    <NoOfPackages >44</NoOfPackages>
                    <PublishedCharge>955.75</PublishedCharge>
                    <IncentiveCredit>-231.08</IncentiveCredit>
                    <BilledCharges>724.67</BilledCharges>
               </SummaryInformation>
                <SummaryInformation >
                    <PickupDate>10/29/1977</PickupDate>
                    <PickUpRecord Pickupid="6481802191">
                    <Details>
                    </Details>
                    </PickUpRecord>    
                    <NoOfPackages >44</NoOfPackages>
                    <PublishedCharge>955.75</PublishedCharge>
                    <IncentiveCredit>-231.08</IncentiveCredit>
                    <BilledCharges>724.67</BilledCharges>
               </SummaryInformation>
                <SummaryInformation >
                    <PickupDate>10/29/2055</PickupDate>
                    <PickUpRecord Pickupid="6481802191">
                    <Details>
                    </Details>
                    </PickUpRecord>    
                    <NoOfPackages >44</NoOfPackages>
                    <PublishedCharge>955.75</PublishedCharge>
                    <IncentiveCredit>-231.08</IncentiveCredit>
                    <BilledCharges>724.67</BilledCharges>
               </SummaryInformation>
                <SummaryInformation >
                    <PickupDate>10/29/2033</PickupDate>
                    <PickUpRecord Pickupid="6481802191">
                    <Details>
                    </Details>
                    </PickUpRecord>    
                    <NoOfPackages >44</NoOfPackages>
                    <PublishedCharge>955.75</PublishedCharge>
                    <IncentiveCredit>-231.08</IncentiveCredit>
                    <BilledCharges>724.67</BilledCharges>
               </SummaryInformation>
                <SummaryInformation >
                    <PickupDate>10/29/2002</PickupDate>
                    <PickUpRecord Pickupid="6481802191">
                    <Details>
                    </Details>
                    </PickUpRecord>    
                    <NoOfPackages >44</NoOfPackages>
                    <PublishedCharge>955.75</PublishedCharge>
                    <IncentiveCredit>-231.08</IncentiveCredit>
                    <BilledCharges>724.67</BilledCharges>
               </SummaryInformation>
                <SummaryInformation >
                    <PickupDate>10/29/2003</PickupDate>
                    <PickUpRecord Pickupid="6481802191">
                    <Details>
                    </Details>
                    </PickUpRecord>    
                    <NoOfPackages >44</NoOfPackages>
                    <PublishedCharge>955.75</PublishedCharge>
                    <IncentiveCredit>-231.08</IncentiveCredit>
                    <BilledCharges>724.67</BilledCharges>
               </SummaryInformation>
</main>


XSD(FileName : XSDSchema1.xsd) :

<xs:schema id="main" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
      <xs:element name="main" msdata:IsDataSet="true">
            <xs:complexType>
                  <xs:choice maxOccurs="unbounded">
                        <xs:element name="SummaryInformation">
                              <xs:complexType>
                                    <xs:sequence>
                                          <xs:element name="PickupDate" type="xs:date" minOccurs="0" />
                                          <xs:element name="NoOfPackages" type="xs:integer" minOccurs="0" />
                                          <xs:element name="PublishedCharge" type="xs:decimal" minOccurs="0" />
                                          <xs:element name="IncentiveCredit" type="xs:decimal" minOccurs="0" />
                                          <xs:element name="BilledCharges" type="xs:decimal" minOccurs="0" />
                                    </xs:sequence>
                              </xs:complexType>
                        </xs:element>
                  </xs:choice>
            </xs:complexType>
      </xs:element>
</xs:schema>


This code is under PAGELOAD

DataSet xx=new DataSet();
                  xx.ReadXml(Server.MapPath("XMLFile1.xml"));
                  xx.ReadXmlSchema(Server.MapPath("XSDSchema1.xsd"));
                  
                  int count1=xx.Tables[0].Rows.Count;
                  string tablename=xx.Tables[0].TableName;

                  foreach( DataColumn dc in xx.Tables[0].Columns )
                  {
                        Response.Write(dc.ColumnName+" , "+dc.DataType +" ,"+dc.Unique +" ,"+dc.AutoIncrement+" ,"+dc.AllowDBNull );
                        Response.Write("<br>");

                  }

OutPut :

PickupDate , System.String ,False ,False ,True
NoOfPackages , System.String ,False ,False ,True
PublishedCharge , System.String ,False ,False ,True
IncentiveCredit , System.String ,False ,False ,True
BilledCharges , System.String ,False ,False ,True
SummaryInformation_Id , System.Int32 ,True ,True ,False
Avatar of AerosSaga
AerosSaga

its displaying datatype as system.string or system.int32?

Aeros
Response.Write(dc.ColumnName+" , "***+dc.DataType*** +" ,"+dc.Unique +" ,"+dc.AutoIncrement+" ,"+dc.AllowDBNull );
Avatar of pprasadravi

ASKER

Hi Aero
it is displaying "System.String" for all columns.At the bottom we have screen out put.Can you pls look that.
Thanks
Ravi
for the columns .datatype which is what it should do
what is the wrong in my code
What do you want there?  Its displaying exactly what your asking it for a .datatype thus System.String or System.Int32 the DATATYPE of the column.
just hit dc. to see what properties you can expose.
Here are your choices:

The properties of the DataColumn class are listed here. For a complete list of DataColumn class members, see the DataColumn Members topic.
Public Properties
public propertyAllowDBNull

Supported by the .NET Compact Framework.
      Gets or sets a value indicating whether null values are allowed in this column for rows belonging to the table.
public propertyAutoIncrement

Supported by the .NET Compact Framework.
      Gets or sets a value indicating whether the column automatically increments the value of the column for new rows added to the table.
public propertyAutoIncrementSeed

Supported by the .NET Compact Framework.
      Gets or sets the starting value for a column that has its AutoIncrement property set to true.
public propertyAutoIncrementStep

Supported by the .NET Compact Framework.
      Gets or sets the increment used by a column with its AutoIncrement property set to true.
public propertyCaption

Supported by the .NET Compact Framework.
      Gets or sets the caption for the column.
public propertyColumnMapping

Supported by the .NET Compact Framework.
      Gets or sets the MappingType of the column.
public propertyColumnName

Supported by the .NET Compact Framework.
      Gets or sets the name of the column in the DataColumnCollection.
public propertyContainer (inherited from MarshalByValueComponent)       Gets the container for the component.
public propertyDataType

Supported by the .NET Compact Framework.
      Gets or sets the type of data stored in the column.
public propertyDefaultValue

Supported by the .NET Compact Framework.
      Gets or sets the default value for the column when creating new rows.
public propertyDesignMode (inherited from MarshalByValueComponent)       Gets a value indicating whether the component is currently in design mode.
public propertyExpression

Supported by the .NET Compact Framework.
      Gets or sets the expression used to filter rows, calculate the values in a column, or create an aggregate column.
public propertyExtendedProperties

Supported by the .NET Compact Framework.
      Gets the collection of custom user information associated with a DataColumn.
public propertyMaxLength

Supported by the .NET Compact Framework.
      Gets or sets the maximum length of a text column.
public propertyNamespace

Supported by the .NET Compact Framework.
      Gets or sets the namespace of the DataColumn.
public propertyOrdinal

Supported by the .NET Compact Framework.
      Gets the position of the column in the DataColumnCollection collection.
public propertyPrefix

Supported by the .NET Compact Framework.
      Gets or sets an XML prefix that aliases the namespace of the DataTable.
public propertyReadOnly

Supported by the .NET Compact Framework.
      Gets or sets a value indicating whether the column allows changes once a row has been added to the table.
public propertySite (inherited from MarshalByValueComponent)       Gets or sets the site of the component.
public propertyTable

Supported by the .NET Compact Framework.
      Gets the DataTable to which the column belongs to.
public propertyUnique

Supported by the .NET Compact Framework.
      Gets or sets a value indicating whether the values in each row of the column must be unique.
Protected Properties
protected propertyEvents (inherited from MarshalByValueComponent)       Gets the list of event handlers that are attached to this component.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDataDataColumnPropertiesTopic.asp
public propertyDataType

Supported by the .NET Compact Framework.
     Gets or sets the type of data stored in the column.

Thats why you were getting dataype you were asking for it
when i am displaying datatable data types it is displaying system.string.
But i mentioned exact datatypes in schema.

                                  <xs:element name="PickupDate" type="xs:date" minOccurs="0" />
                                   <xs:element name="NoOfPackages" type="xs:integer" minOccurs="0" />
                                   <xs:element name="PublishedCharge" type="xs:decimal" minOccurs="0" />
                                   <xs:element name="IncentiveCredit" type="xs:decimal" minOccurs="0" />
                                   <xs:element name="BilledCharges" type="xs:decimal" minOccurs="0" />

when i disply the datatypes
 Response.Write(dc.ColumnName+" , "+dc.DataType +" ,"+dc.Unique +" ,"+dc.AutoIncrement+" ,"+dc.AllowDBNull );

PickupDate , System.String ,False ,False ,True
NoOfPackages , System.String ,False ,False ,True
PublishedCharge , System.String ,False ,False ,True
IncentiveCredit , System.String ,False ,False ,True
BilledCharges , System.String ,False ,False ,True
SummaryInformation_Id , System.Int32 ,True ,True ,False

thanks
Ravi
evidently when the framework renders the xml it renders it with those types.  If you want the underlying type query the schema, instead of using response.write

when it renders it for html output that is
agreed.but when i am sorting on dataview it is taking all  column  datatype as string.
but i don't have databse.i getting data from xml.
[Visual Basic]
Private Shared Sub DemonstrateGetXmlSchema()
    ' Create a DataSet with one table containing two columns.
    Dim ds As DataSet = New DataSet("myDataSet")
    Dim t As DataTable = ds.Tables.Add("Items")
    t.Columns.Add("id", Type.GetType("System.Int32"))
    t.Columns.Add("Item", Type.GetType("System.String"))

    ' Display the DataSet schema as XML.
    Console.WriteLine( ds.GetXmlSchema() )
End Sub

[C#]
private static void DemonstrateGetXmlSchema()
{
    // Create a DataSet with one table containing two columns.
    DataSet ds = new DataSet("myDataSet");
    DataTable t = ds.Tables.Add("Items");
    t.Columns.Add("id", typeof(int));
    t.Columns.Add("Item", typeof(string));

    // Display the DataSet schema as XML.
    Console.WriteLine( ds.GetXmlSchema() );
}
I said dataset,

Youve already got it in a dataset come on man

DataSet xx=new DataSet();
               xx.ReadXml(Server.MapPath("XMLFile1.xml"));
               xx.ReadXmlSchema(Server.MapPath("XSDSchema1.xsd"));
               
               int count1=xx.Tables[0].Rows.Count;
               string tablename=xx.Tables[0].TableName;

               foreach( DataColumn dc in xx.Tables[0].Columns )
               {
                    Response.Write(dc.ColumnName+" , "+dc.DataType +" ,"+dc.Unique +" ,"+dc.AutoIncrement+" ,"+dc.AllowDBNull );
                    Response.Write("<br>");

               }
[Visual Basic]
custDS.WriteXmlSchema("Customers.xsd")
[C#]
custDS.WriteXmlSchema("Customers.xsd");
[Visual Basic]
Dim xmlSW As System.IO.StreamWriter = New System.IO.StreamWriter("Customers.xsd")
custDS.WriteXmlSchema(xmlSW)
xmlSW.Close()
[C#]
System.IO.StreamWriter xmlSW = new System.IO.StreamWriter("Customers.xsd");
custDS.WriteXmlSchema(xmlSW);
xmlSW.Close();
To obtain the schema of a DataSet and write it as an XML Schema string, use the GetXmlSchema method as shown in the following example.

[Visual Basic]
Dim xsdDS As String = custDS.GetXmlSchema()
[C#]
string xsdDS = custDS.GetXmlSchema();
Response.Write(dc.ColumnName+" , "+dc.DataType +" ,"+dc.Unique +" ,"+dc.AutoIncrement+" ,"+dc.AllowDBNull );
 displying system.string for all columns.

I am really sorry aero ,today i totally stressedout.
i have to complete this task by endof day

I am really sorry for that.
just relax man, take a breath, you've already done the hard part reading the xml into a dataset with xsd schema, try that last one:
Write from the schema file
 [Visual Basic]
custDS.WriteXmlSchema("Customers.xsd")
[C#]
custDS.WriteXmlSchema("Customers.xsd");
[Visual Basic]
Dim xmlSW As System.IO.StreamWriter = New System.IO.StreamWriter("Customers.xsd")
custDS.WriteXmlSchema(xmlSW)
xmlSW.Close()
[C#]
System.IO.StreamWriter xmlSW = new System.IO.StreamWriter("Customers.xsd");
custDS.WriteXmlSchema(xmlSW);
xmlSW.Close();
To obtain the schema of a DataSet and write it as an XML Schema string, use the GetXmlSchema method as shown in the following example.

[Visual Basic]
Dim xsdDS As String = custDS.GetXmlSchema()
[C#]
string xsdDS = custDS.GetXmlSchema();
Hi Aero

i found the probelm.
the date format is not accepting .Do you have any clue.


Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.FormatException: String was not recognized as a valid DateTime.

Source Error:


Line 30:                   
Line 31:                   xx.ReadXmlSchema(Server.MapPath("XSDSchema1.xsd"));
Line 32:                   xx.ReadXml(Server.MapPath("XMLFile1.xml"));
Line 33:                   
Line 34:                   
 

Thanks
Ravi
ASKER CERTIFIED SOLUTION
Avatar of AerosSaga
AerosSaga

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Accepted.I am giving points Aero.I will do research on date format.Whatever dateformat i specified it is not accepting.
i have another question.When element have attribute it is creating another table in dataset.Can't we create attribute in the same table.

Question may not be clear

In my xml PickUpRecord has attribute.So it is creating two tables
SummaryInformation and PickUpRecord.Can't we do it in one table even if we have attributes.

Thanks
Ravi
I believe that extra table is needed for the schema .xsd
no i want to create the followinde as one record in my table.So if we want we can chage schema.
<main>
<SummaryInformation >
                    <PickupDate>10/29/2003</PickupDate>
                    <PickUpRecord Pickupid="6481802191">
                    <Details>
                    </Details>
                    </PickUpRecord>    
                    <NoOfPackages >44</NoOfPackages>
                    <PublishedCharge>955.75</PublishedCharge>
                    <IncentiveCredit>-231.08</IncentiveCredit>
                    <BilledCharges>724.67</BilledCharges>
</SummaryInformation>
</main>
what are the two tables its creating?
Summaryinformation and pickuprecord

pickupid is the column for pickuprecordtable.

try making your initial xml something like:
<main>
                    <PickUpRecord Pickupid="6481802191">
                     <PickupDate>10/29/2003</PickupDate>
                    <Details>
                    </Details>
                    <NoOfPackages >44</NoOfPackages>
                    <PublishedCharge>955.75</PublishedCharge>
                    <IncentiveCredit>-231.08</IncentiveCredit>
                    <BilledCharges>724.67</BilledCharges>
                    </PickUpRecord>    
</main>
i am sorry aero.It is rellay good solution but all elements are in the same level.I can't chage it.
well thats why your schema looks the way it does your only option then is to create a view of the dataset/schema to make it look like you want, seee my other posts in your ongoing question.
agreed how can i create dataview on multiple tables.
moving to the open question now. check there I already posted the links
https://www.experts-exchange.com/questions/21149631/xml-xsl-and-dataset.html
Ok, I think you'll need to create a view of the schema and join the two tables before you bind then:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnsqlmag02/html/definingxmlviews.asp