By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,816 Members | 2,151 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,816 IT Pros & Developers. It's quick & easy.

Problem with reading XML

P: n/a
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.ReadNoCollectTextTo ken()
at System.Xml.XmlValidatingReader.Read()
at System.Xml.XPath.XPathDocument.ReadChildNodes(XPat hContainer
parent, Strin
g parentBaseUri, XmlReader reader, PositionInfo positionInfo)
at System.Xml.XPath.XPathDocument.ReadChildNodes(XPat hContainer
parent, Strin
g parentBaseUri, XmlReader reader, PositionInfo positionInfo)
at System.Xml.XPath.XPathDocument.ReadChildNodes(XPat hContainer
parent, Strin
g parentBaseUri, XmlReader reader, PositionInfo positionInfo)
at System.Xml.XPath.XPathDocument.ReadChildNodes(XPat hContainer
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

Mar 15 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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

Mar 15 '06 #2

P: n/a
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_bcc24befeac 6"
ss:RefersTo="=Sheet1!R1C1:R4C3" />
<NamedRange ss:Name="Reify_09021612_5a3c_4188_8fd2_bcc24befeac 6.C0"
ss:RefersTo="=Sheet1!R1C1:R4C1" />
<NamedRange ss:Name="Reify_09021612_5a3c_4188_8fd2_bcc24befeac 6.C1"
ss:RefersTo="=Sheet1!R1C2:R4C2" />
<NamedRange ss:Name="Reify_09021612_5a3c_4188_8fd2_bcc24befeac 6.C2"
ss:RefersTo="=Sheet1!R1C3:R4C3" />
<NamedRange ss:Name="Reify_09021612_5a3c_4188_8fd2_bcc24befeac 6.R0"
ss:RefersTo="=Sheet1!R1C1:R1C3" />
<NamedRange ss:Name="Reify_09021612_5a3c_4188_8fd2_bcc24befeac 6.R1"
ss:RefersTo="=Sheet1!R2C1:R2C3" />
<NamedRange ss:Name="Reify_09021612_5a3c_4188_8fd2_bcc24befeac 6.R2"
ss:RefersTo="=Sheet1!R3C1:R3C3" />
<NamedRange ss:Name="Reify_09021612_5a3c_4188_8fd2_bcc24befeac 6.R3"
ss:RefersTo="=Sheet1!R4C1:R4C3" />
<NamedRange ss:Name="Reify_4fd3e104_67ff_4ac7_95a6_4a34dd3251e 1"
ss:RefersTo="=Sheet1!R2C2:R3C3" />
<NamedRange ss:Name="Reify_4fd3e104_67ff_4ac7_95a6_4a34dd3251e 1.C0"
ss:RefersTo="=Sheet1!R2C2:R3C2" />
<NamedRange ss:Name="Reify_4fd3e104_67ff_4ac7_95a6_4a34dd3251e 1.C1"
ss:RefersTo="=Sheet1!R2C3:R3C3" />
<NamedRange ss:Name="Reify_4fd3e104_67ff_4ac7_95a6_4a34dd3251e 1.R0"
ss:RefersTo="=Sheet1!R2C2:R2C3" />
<NamedRange ss:Name="Reify_4fd3e104_67ff_4ac7_95a6_4a34dd3251e 1.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_bcc24befeac 6.R0"
/>
<NamedCell ss:Name="Reify_09021612_5a3c_4188_8fd2_bcc24befeac 6.C0"
/>
<NamedCell ss:Name="Reify_09021612_5a3c_4188_8fd2_bcc24befeac 6" />
</Cell>
</Row>
<Row>
<Cell ss:Index="2">
<Data ss:Type="String">a</Data>
<NamedCell ss:Name="Reify_09021612_5a3c_4188_8fd2_bcc24befeac 6.R1"
/>
<NamedCell ss:Name="Reify_4fd3e104_67ff_4ac7_95a6_4a34dd3251e 1.R0"
/>
<NamedCell ss:Name="Reify_4fd3e104_67ff_4ac7_95a6_4a34dd3251e 1.C0"
/>
<NamedCell ss:Name="Reify_4fd3e104_67ff_4ac7_95a6_4a34dd3251e 1" />
<NamedCell ss:Name="Reify_09021612_5a3c_4188_8fd2_bcc24befeac 6.C1"
/>
<NamedCell ss:Name="Reify_09021612_5a3c_4188_8fd2_bcc24befeac 6" />
</Cell>
</Row>
<Row>
<Cell ss:Index="3">
<Data ss:Type="String">a</Data>
<NamedCell ss:Name="Reify_4fd3e104_67ff_4ac7_95a6_4a34dd3251e 1" />
<NamedCell ss:Name="Reify_4fd3e104_67ff_4ac7_95a6_4a34dd3251e 1.C1"
/>
<NamedCell ss:Name="Reify_09021612_5a3c_4188_8fd2_bcc24befeac 6.R2"
/>
<NamedCell ss:Name="Reify_4fd3e104_67ff_4ac7_95a6_4a34dd3251e 1.R1"
/>
<NamedCell ss:Name="Reify_09021612_5a3c_4188_8fd2_bcc24befeac 6" />
<NamedCell ss:Name="Reify_09021612_5a3c_4188_8fd2_bcc24befeac 6.C2"
/>
</Cell>
</Row>
<Row>
<Cell ss:Index="3">
<Data ss:Type="String">v</Data>
<NamedCell ss:Name="Reify_09021612_5a3c_4188_8fd2_bcc24befeac 6" />
<NamedCell ss:Name="Reify_09021612_5a3c_4188_8fd2_bcc24befeac 6.C2"
/>
<NamedCell ss:Name="Reify_09021612_5a3c_4188_8fd2_bcc24befeac 6.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

Mar 15 '06 #3

P: n/a
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/de...edPrefixes.asp

Mar 16 '06 #4

P: n/a
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();

Mar 16 '06 #5

P: n/a
Hi Dickster ,
Thanks for replaying. Now its working.

Thanks,
Prasanta

Mar 17 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.