Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / February 2006

Tip: Looking for answers? Try searching our database.

Excel (in XML format) to a dataset

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
sandeshmeda@gmail.com - 15 Feb 2006 21:30 GMT
I have a XML file that is basically a Excel file saved as XML. I need
to be able to change the XML into a different format.

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
Darren Kopp - 15 Feb 2006 22:05 GMT
I'm unfamiliar with the format that XML saves in, so if you could post
some it that would help.  As for the WriteXML, it just writes out the
data in the data table (which may be the same format as what you
imported, I have not tried this).

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/ce81
6696cabf590e
)
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/
sloan - 15 Feb 2006 22:52 GMT
Check my blog:
http://spaces.msn.com/sholliday/

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>
kahtava@gmail.com - 15 Feb 2006 22:59 GMT
What format would you like to change your XML file into?

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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.