I was thinking one approach would be to:
1. Populate the dataset based on the XML file I get from excel
2. Use a WriteXML to create a XML file in the format I need.
Will this work? How do I get started on this? Are there any other
alternate ways of doing this?
TIA
You could try changing the schema or loading a new schema on the
dataset and then calling the WriteXML Method, but I don't know if that
would work. When I have needed to control the format of an XML
document, I have written up my own logic to parse and write the xml
file.
I answered another DataSet question about writing xml here
(http://groups.google.com/group/microsoft.public.dotnet.framework.adonet/msg/ce816696cabf590e)
but I don't know if that will help you out.
I would model the format you want (with lists of CollectionBase) and
then parse the xml data and place it into the appropriate location,
then write out the data.
Really hope this helps you out somehow,
Darren Kopp
http://blog.secudocs.com/
9/22/2005
You need to do an XML to XML transformation.
The format of the Excel XML is .... unique. It also has namespaces. So
there isn't a super trivial example.
I have some code below. I think it will work, but it will definately get
you going.
It took me about 2 weeks to figure this out, so post a 'thank you' please,
(and any issues if you run into them)
Ok..
Create an excel file.
3 columns, the first row is the header.
uid,lastname,firstname
Add like 4 more rows of real data ( "123","Smith","John" .. like that)
Save as XML.
I have the C# code, which will show how to apply the XSL to the XML.
There is xsl also. Hopefully, it won't get screwed up
///////////////// START C# code
/// <summary>
/// Summary description for XMLtoXSLTransformWrapper.
/// </summary>
public class XMLtoXSLTransformWrapper
{
string debugMsg=null;
public void DoTranslation(string xmlFile, string xslFile, string
outputFile)
{
try
{
//Create a new XslTransform object.
XslTransform xslt = new XslTransform();
//Load the stylesheet.
xslt.Load(xslFile);
//Create a new XPathDocument and load the XML data to be transformed.
XPathDocument mydata = new XPathDocument(xmlFile);
//Create an XmlTextWriter which outputs to the console.
//XmlWriter writer = new XmlTextWriter(Console.Out);
//Transform the data and send the output to the console.
//xslt.Transform(mydata,null,writer, null);
xslt.Transform (xmlFile, outputFile);
}
catch (Exception ex)
{
debugMsg = ex.Message;
Console.WriteLine (debugMsg);
}
}
public XMLtoXSLTransformWrapper()
{
//
// TODO: Add constructor logic here
//
}
}
}
//END C#
//START XSL
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" >
<xsl:template match="/">
<OneWellDefinedDS>
<!--<myRootPosition><xsl:value-of select="position()"/></myRootPosition>-->
<xsl:apply-templates select="ss:Workbook"/>
</OneWellDefinedDS>
</xsl:template>
<xsl:template match="ss:Workbook">
<!--<myWorkbookPosition><xsl:value-of
select="position()"/></myWorkbookPosition>-->
<xsl:apply-templates select="ss:Worksheet"/>
</xsl:template>
<xsl:template match="ss:Worksheet">
<!--<myWorksheetPosition><xsl:value-of
select="position()"/></myWorksheetPosition>-->
<xsl:choose>
<xsl:when test="position()=1">
<xsl:apply-templates select="ss:Table"/>
</xsl:when>
<xsl:otherwise>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="ss:Table">
<!--<myTablePosition><xsl:value-of
select="position()"/></myTablePosition>-->
<xsl:apply-templates select="ss:Row"/>
</xsl:template>
<xsl:template match="ss:Row">
<!--<myRowPosition><xsl:value-of select="position()"/></myRowPosition>-->
<xsl:choose>
<xsl:when test="position()=1">
</xsl:when>
<xsl:otherwise>
<customerInfo>
<!--<myPosition><xsl:value-of select="position()"/></myPosition>-->
<xsl:apply-templates select="ss:Cell"/>
</customerInfo>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="ss:Cell">
<!--<myCellPosition><xsl:value-of
select="$varCurrentAttributePosition"/></myCellPosition>-->
<xsl:choose>
<xsl:when test="position()=1">
<xsl:call-template name="xuid" />
</xsl:when>
<xsl:when test="position()=2">
<xsl:call-template name="xlastname" />
</xsl:when>
<xsl:when test="position()=3">
<xsl:call-template name="xfirstname" />
</xsl:when>
<xsl:otherwise>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="xuid">
<uid><xsl:value-of select="."/></uid>
</xsl:template>
<xsl:template name="xlastname">
<lastname><xsl:value-of select="."/></lastname>
</xsl:template>
<xsl:template name="xfirstname">
<firstname><xsl:value-of select="."/></firstname>
</xsl:template>
</xsl:stylesheet>
Are you converting from XML to Excel file?
If so; just open your XML file in Excel, Excel will display the XML
file as an Excel sheet.
Your approach sounds feasible; the dataset will allow you to loop
through the tables, rows and columns. You can then generate a file
(StreamWriter) with your desired format / extension.