Click here to Skip to main content
15,898,865 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I am converting a data set into XML and validating the contents and data type using XSD. My problem is here in my XML if I pass some wrong input say '@' in one of my column, It catches the error but shows that error is in precedding row means if error is in 8th row of dataset it shows the error row is 7. Here is my XML m trying to validate:
HTML
<Table1>
    <F1>E405</F1>
    <F2>0.3623</F2>
  </Table1>
  <Table1>
    <F1>E406</F1>
    <F2>0.5</F2>
  </Table1>
  <Table1>
    <F1>E407@@</F1>
    <F2>0.3</F2>
  </Table1>
  <Table1>
    <F1>E409</F1>
    <F2>0.4</F2>
  </Table1>
.

And my xsd:
XML
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="NewDataSet">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="unbounded" name="Table1">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="F1">
                <xs:simpleType>
                  <xs:restriction base="xs:string">
                    <xs:pattern value="[^*+.@%,)(!]+">
                      
                    </xs:pattern>
                  </xs:restriction>
                  
                </xs:simpleType>
                
              </xs:element>
              <xs:element name="F2">
                <xs:simpleType>
                  <xs:restriction base="xs:decimal">
                  

                  </xs:restriction>
                </xs:simpleType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
...
....
..

Based on my logic i get the error rows in  a list and highlite those rows in my grid accordingly. But coz of this problem I am unable to get the correct row number:
it will highlight the "E406" but not "E407".

Please help !
Posted

1 solution

XML
public string GetDataStringXml(DataSet objToXML)
   {
       StringBuilder strXml = new StringBuilder();
       strXml.Append("<?xml version='1.0' encoding='utf-8' ?>");
       strXml.Append("<" + objToXML.DataSetName + ">");
       foreach (DataRow dataRow in objToXML.Tables[0].Rows)
       {
           strXml.Append("<" + objToXML.Tables[0].TableName + ">");
           foreach (DataColumn dataColumn in objToXML.Tables[0].Columns)
           {
               if (dataRow[dataColumn].ToString() == "")
               {

                   strXml.Append("<" + dataColumn.ColumnName + ">" +
                                 GetDefault(dataColumn.DataType) +
                              "</" + dataColumn.ColumnName + ">");

               }
               else
               {
                   strXml.Append("<" + dataColumn.ColumnName + ">" +
                                  dataRow[dataColumn] +
                             "</" + dataColumn.ColumnName + ">");
               }
           }

           strXml.Append("</" + objToXML.Tables[0].TableName + ">");
       }

       strXml.Append("</" + objToXML.DataSetName + ">");


       return strXml.ToString();



   }




Solved it myself..Created this function which adds the default value for the column if its empty and keeps the count of rows constant.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900