473,385 Members | 1,325 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Problem with reading XML

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
5 2091
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
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
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
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
Hi Dickster ,
Thanks for replaying. Now its working.

Thanks,
Prasanta

Mar 17 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Magnus Lycka | last post by:
I'm trying to read standard out in a process started with popen2 in a non-blocking way. (Other good ways of doing this than the one I tried are appreciated.) I've tried to dumb down my code to...
7
by: Thomas Sourmail | last post by:
Hi, I hope I am missing something simple, but.. here is my problem: I need my program to check the last column of a file, as in : a b c d target ref 0 0 0 0 1 a 1 0 0 0 1.5 b 2 0 0 0 2 c
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
0
by: Fabrice | last post by:
Hello, (Alain) Tis is a part of my code to retrieve text from hastable in memory cache, by reading (befor) a resources file. Thanks for your help. /1/ The resources file * I have create a...
7
by: DBMS_Plumber | last post by:
Having a hard time with a UDF. Wonder if folk might care to take a look and tell me what's up. All of this kicked off from trying to build a set of C++ iostream derived classes that can make...
1
by: Pathik | last post by:
Hi All, I have to get the values of "reading" and "value" elements of context Person/Category/Group/ser.These values must be on condition based,means I have to get the values of "reading" and...
11
by: aljaber | last post by:
hi, i am facing a problem with my program output here is the program /*********************************************\ * CD Database * * ...
5
blazedaces
by: blazedaces | last post by:
Ok, so you know my problem, java is running out of memory reading with SAX, the event-based xml parser intended more-so than DOM for extremely large files. I'll try to explain what I've been doing...
6
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features...
10
by: oktayarslan | last post by:
Hi all; I have a problem when inserting an element to a vector. All I want is reading some data from a file and putting them into a vector. But the program is crashing after pushing a data which...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.