Home All Groups Group Topic Archive Search About

Problem with reading XML

Author
15 Mar 2006 6:24 AM
prasanta
Hello,
I have a xml file. when i am trying to load through XPathDocument and
try to get XPathNodeIterator its not working . my xml is loking like

<?xml version="1.0" ?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
    xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
    <Styles>
        <Style ss:ID="Default" ss:Name="Normal">
            <Alignment ss:Vertical="Bottom" />
            <Borders />
            <Font />
            <Interior />
            <NumberFormat />
            <Protection />
        </Style>
        <Style ss:ID="s22">
            <Font x:Family="Swiss" ss:Bold="1" />
        </Style>
    </Styles>
</Workbook>

Code:
XPathDocument xpDoc = new XPathDocument(path);
                XPathNavigator xpNav = xpDoc.CreateNavigator();
                XPathNodeIterator xpite =
xpNav.Select("/Workbook/Styles/Style/@ID");
                Console.WriteLine("No of item::"+xpite.Count.ToString());
                while(xpite.MoveNext())
                {
                    Console.WriteLine(xpite.Current.Value);
                }

But with this code i am not getting any result and its thrown exception
as

Xpath:System.Xml.XmlException: This is an unexpected token. The
expected token i
s 'NAME'. Line 14, position 15.
   at System.Xml.XmlTextReader.SetAttributeValues()
   at System.Xml.XmlTextReader.ParseElement()
   at System.Xml.XmlTextReader.Read()
   at System.Xml.XmlValidatingReader.ReadNoCollectTextToken()
   at System.Xml.XmlValidatingReader.Read()
   at System.Xml.XPath.XPathDocument.ReadChildNodes(XPathContainer
parent, Strin
g parentBaseUri, XmlReader reader, PositionInfo positionInfo)
   at System.Xml.XPath.XPathDocument.ReadChildNodes(XPathContainer
parent, Strin
g parentBaseUri, XmlReader reader, PositionInfo positionInfo)
   at System.Xml.XPath.XPathDocument.ReadChildNodes(XPathContainer
parent, Strin
g parentBaseUri, XmlReader reader, PositionInfo positionInfo)
   at System.Xml.XPath.XPathDocument.ReadChildNodes(XPathContainer
parent, Strin
g parentBaseUri, XmlReader reader, PositionInfo positionInfo)
   at System.Xml.XPath.XPathDocument.Load(XmlReader reader)
   at System.Xml.XPath.XPathDocument.Init(XmlReader reader)
   at System.Xml.XPath.XPathDocument..ctor(String uri)
   at test.Class1.cellDiff(String path) in c:\test\class1.cs:line 70

But if i modify the xml look like

<Workbook>
    <Styles>
        <Style ID="Default" Name="Normal">
            <Alignment Vertical="Bottom" />
            <Borders />
            <Font />
            <Interior />
            <NumberFormat />
            <Protection />
        </Style>
        <Style ID="s22">
            <Font Family="Swiss" Bold="1" />
        </Style>
    </Styles>
</Workbook>

and getting result with same code. How can i read above XML without
doing any modification.
Thanks,
Prasanta

Author
15 Mar 2006 12:54 PM
dickster
Not 100% sure what you are hoping to see and cannot regenerate that
error.

However is the xpath expression your looking:

/Workbook/Styles/Style/@ss:ID

Dickster
Author
15 Mar 2006 3:01 PM
prasanta
Hello,
this is my full XML content..
<?xml version="1.0" ?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
    xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
    <Styles>
        <Style ss:ID="Default" ss:Name="Normal">
            <Alignment ss:Vertical="Bottom" />
            <Borders />
            <Font />
            <Interior />
            <NumberFormat />
            <Protection />
        </Style>
        <Style ss:ID="s22">
            <Font x:Family="Swiss" ss:Bold="1" />
        </Style>
    </Styles>
    <Worksheet ss:Name="Sheet1">
        <Names>
            <NamedRange ss:Name="Reify_09021612_5a3c_4188_8fd2_bcc24befeac6"
ss:RefersTo="=Sheet1!R1C1:R4C3" />
            <NamedRange ss:Name="Reify_09021612_5a3c_4188_8fd2_bcc24befeac6.C0"
ss:RefersTo="=Sheet1!R1C1:R4C1" />
            <NamedRange ss:Name="Reify_09021612_5a3c_4188_8fd2_bcc24befeac6.C1"
ss:RefersTo="=Sheet1!R1C2:R4C2" />
            <NamedRange ss:Name="Reify_09021612_5a3c_4188_8fd2_bcc24befeac6.C2"
ss:RefersTo="=Sheet1!R1C3:R4C3" />
            <NamedRange ss:Name="Reify_09021612_5a3c_4188_8fd2_bcc24befeac6.R0"
ss:RefersTo="=Sheet1!R1C1:R1C3" />
            <NamedRange ss:Name="Reify_09021612_5a3c_4188_8fd2_bcc24befeac6.R1"
ss:RefersTo="=Sheet1!R2C1:R2C3" />
            <NamedRange ss:Name="Reify_09021612_5a3c_4188_8fd2_bcc24befeac6.R2"
ss:RefersTo="=Sheet1!R3C1:R3C3" />
            <NamedRange ss:Name="Reify_09021612_5a3c_4188_8fd2_bcc24befeac6.R3"
ss:RefersTo="=Sheet1!R4C1:R4C3" />
            <NamedRange ss:Name="Reify_4fd3e104_67ff_4ac7_95a6_4a34dd3251e1"
ss:RefersTo="=Sheet1!R2C2:R3C3" />
            <NamedRange ss:Name="Reify_4fd3e104_67ff_4ac7_95a6_4a34dd3251e1.C0"
ss:RefersTo="=Sheet1!R2C2:R3C2" />
            <NamedRange ss:Name="Reify_4fd3e104_67ff_4ac7_95a6_4a34dd3251e1.C1"
ss:RefersTo="=Sheet1!R2C3:R3C3" />
            <NamedRange ss:Name="Reify_4fd3e104_67ff_4ac7_95a6_4a34dd3251e1.R0"
ss:RefersTo="=Sheet1!R2C2:R2C3" />
            <NamedRange ss:Name="Reify_4fd3e104_67ff_4ac7_95a6_4a34dd3251e1.R1"
ss:RefersTo="=Sheet1!R3C2:R3C3" />
        </Names>
        <Table ss:ExpandedColumnCount="3" ss:ExpandedRowCount="4">
            <Row>
                <Cell ss:StyleID="s22">
                    <Data ss:Type="String">v</Data>
                    <NamedCell ss:Name="Reify_09021612_5a3c_4188_8fd2_bcc24befeac6.R0"
/>
                    <NamedCell ss:Name="Reify_09021612_5a3c_4188_8fd2_bcc24befeac6.C0"
/>
                    <NamedCell ss:Name="Reify_09021612_5a3c_4188_8fd2_bcc24befeac6" />
                </Cell>
            </Row>
            <Row>
                <Cell ss:Index="2">
                    <Data ss:Type="String">a</Data>
                    <NamedCell ss:Name="Reify_09021612_5a3c_4188_8fd2_bcc24befeac6.R1"
/>
                    <NamedCell ss:Name="Reify_4fd3e104_67ff_4ac7_95a6_4a34dd3251e1.R0"
/>
                    <NamedCell ss:Name="Reify_4fd3e104_67ff_4ac7_95a6_4a34dd3251e1.C0"
/>
                    <NamedCell ss:Name="Reify_4fd3e104_67ff_4ac7_95a6_4a34dd3251e1" />
                    <NamedCell ss:Name="Reify_09021612_5a3c_4188_8fd2_bcc24befeac6.C1"
/>
                    <NamedCell ss:Name="Reify_09021612_5a3c_4188_8fd2_bcc24befeac6" />
                </Cell>
            </Row>
            <Row>
                <Cell ss:Index="3">
                    <Data ss:Type="String">a</Data>
                    <NamedCell ss:Name="Reify_4fd3e104_67ff_4ac7_95a6_4a34dd3251e1" />
                    <NamedCell ss:Name="Reify_4fd3e104_67ff_4ac7_95a6_4a34dd3251e1.C1"
/>
                    <NamedCell ss:Name="Reify_09021612_5a3c_4188_8fd2_bcc24befeac6.R2"
/>
                    <NamedCell ss:Name="Reify_4fd3e104_67ff_4ac7_95a6_4a34dd3251e1.R1"
/>
                    <NamedCell ss:Name="Reify_09021612_5a3c_4188_8fd2_bcc24befeac6" />
                    <NamedCell ss:Name="Reify_09021612_5a3c_4188_8fd2_bcc24befeac6.C2"
/>
                </Cell>
            </Row>
            <Row>
                <Cell ss:Index="3">
                    <Data ss:Type="String">v</Data>
                    <NamedCell ss:Name="Reify_09021612_5a3c_4188_8fd2_bcc24befeac6" />
                    <NamedCell ss:Name="Reify_09021612_5a3c_4188_8fd2_bcc24befeac6.C2"
/>
                    <NamedCell ss:Name="Reify_09021612_5a3c_4188_8fd2_bcc24befeac6.R3"
/>
                </Cell>
            </Row>
        </Table>
    </Worksheet>
</Workbook>

from that xml i want to iterate all the <Cell> node.
My code is like

string xPath = "/Workbook/Worksheet/Table/Row/Cell";
XPathDocument xpDoc = new XPathDocument(path);
XPathNavigator xpNav = xpDoc.CreateNavigator();
XPathNodeIterator xpite = xpNav.Select(xPath);
Console.WriteLine("No of item::"+xpite.Count.ToString());

When i run this code its showing  No of item::0
I wrote XPath = "/Workbook/Worksheet/Table/Row/Cell" otherway "//Cell"
Both are giving count 0.

Thanks,
Prasanta
Author
16 Mar 2006 9:43 AM
dickster
I reckon its to do with your default names space

i.e. xmlns="urn:schemas-microsoft-com:office:spreadsheet"

check out
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconXPathQueriesWithNamespacedMappedPrefixes.asp
Author
16 Mar 2006 10:38 AM
dickster
Prasanta

I found this a hard bit to understand in that article

"The XmlNamespaceManager allows adding default namespaces by using an
empty string as the prefix. However, XPath treats the empty prefix as
the null namespace. In other words, only prefixes mapped to namespaces
can be used in XPath queries. If you want to use the default namespace
from an XML document in the XPath expression, then you need to define a
prefix for it."

But I think for your code it translates to:

        string xPath =
"/dpx:Workbook/dpx:Worksheet/dpx:Table/dpx:Row/dpx:Cell";
            XPathDocument xpDoc = new XPathDocument(<<path>>);
            XPathNavigator xpNav = xpDoc.CreateNavigator();

            XmlNamespaceManager context = new
XmlNamespaceManager(xpNav.NameTable);

            // Call  dpx - TheDefaultPrefix
            context.AddNamespace("dpx","urn:schemas-microsoft-com:office:spreadsheet");
            context.AddNamespace("ss","urn:schemas-microsoft-com:office:spreadsheet");
            context.AddNamespace("o","urn:schemas-microsoft-com:office:office");
            context.AddNamespace("x","urn:schemas-microsoft-com:office:excel");

            XPathExpression Expr = xpNav.Compile(xPath);
            Expr.SetContext(context);

            XPathNodeIterator xpite = xpNav.Select(Expr);
            Console.WriteLine("No of item::"+xpite.Count.ToString());

            while(xpite.MoveNext())
            {
                Console.WriteLine(xpite.Current.Value);
            }
            Console.Read();
Author
17 Mar 2006 9:12 AM
prasanta
Hi Dickster ,
Thanks for replaying. Now its working.

Thanks,
Prasanta

AddThis Social Bookmark Button