Altova Mailing List Archives>Archive Index >microsoft.public.xml Archive Home >Recent entries >Thread Prev - Re: New to Schemas >Thread Next - Re: New to Schemas Re: New to SchemasTo: NULL Date: 1/4/2008 4:34:00 PM
"Martin Honnen" <mahotrash@y...> wrote in message
news:umrYot7SIHA.4752@T......
> tshad wrote:
>> I am trying to read an xml document into a dataSet and am unsure how to
>> set it up.
>>
>> If I have a document such as:
>>
>> <?xml version="1.0"?>
>> <address country="Italy">
>> <tag name="street" flag="5">One Microsoft Way</tag>
>> <tag name="number">1</tag>
>> <tag name="city">Redmond</tag>
>> <tag name="state" unit="25">WA</tag>
>> <tag name="zip">98052</tag>
>> </address>
>>
>> I want there to be one table: Address and each record would be the "tag"
>> lines. The line would have a name (such as "street" or "city") a value
>> (such as ""One Microsoft Way") and a possible attribute or 2 (such as
>> flag).
>>
>> I would want the table to be something like:
>>
>> street 5 One Microsoft Way
>> number 1
>> city Redmond
>> state 25 WA
>> zip 98052
>
What does the following lines in your code do? Do you need those lines if
you are going to copy this to a dataset?
<dataset>
<xsl:apply-templates/>
</dataset>
Thanks,
Tom
> One way to solve that is to use an XSLT stylesheet to transform the XML to
> the format ReadXml expects. Here is an XSLT stylesheet that does that:
>
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
> <xsl:output method="xml" indent="yes"/>
> <xsl:template match="address">
> <dataset>
> <xsl:apply-templates/>
> </dataset>
> </xsl:template>
> <xsl:template match="tag">
> <address>
> <name>
> <xsl:value-of select="@name"/>
> </name>
> <flag>
> <xsl:if test="not(@flag)">
> <xsl:attribute name="xsi:nil">true</xsl:attribute>
> </xsl:if>
> <xsl:value-of select="@flag"/>
> </flag>
> <unit>
> <xsl:if test="not(@unit)">
> <xsl:attribute name="xsi:nil">true</xsl:attribute>
> </xsl:if>
> <xsl:value-of select="@unit"/>
> </unit>
> <value>
> <xsl:value-of select="."/>
> </value>
> </address>
> </xsl:template>
> </xsl:stylesheet>
>
> You can then apply the transformation with XslCompiledTransform as
> follows:
>
> XslCompiledTransform xsltProcessor = new
> XslCompiledTransform();
> xsltProcessor.Load(@"..\..\XSLTFile1.xslt");
> DataSet ds = new DataSet();
> using (MemoryStream memStream = new MemoryStream())
> {
> xsltProcessor.Transform(@"..\..\XMLFile1.xml", null,
> memStream);
> memStream.Position = 0;
> ds.ReadXml(memStream);
> }
>
> That way you get one table with five rows:
>
> Table address:
> name flag unit value
> street 5 One Microsoft Way
> number 1
> city Redmond
> state 25 WA
> zip 98052
>
>
> --
>
> Martin Honnen --- MVP XML
> http://JavaScript.FAQTs.com/
| ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
