473,839 Members | 1,352 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with reading XML

Hello,
I have a xml file. when i am trying to load through XPathDocument and
try to get XPathNodeIterat or its not working . my xml is loking like

<?xml version="1.0" ?>
<?mso-application progid="Excel.S heet"?>
<Workbook xmlns="urn:sche mas-microsoft-com:office:spre adsheet"
xmlns:o="urn:sc hemas-microsoft-com:office:offi ce"
xmlns:x="urn:sc hemas-microsoft-com:office:exce l"
xmlns:ss="urn:s chemas-microsoft-com:office:spre adsheet"
xmlns:html="htt p://www.w3.org/TR/REC-html40">
<Styles>
<Style ss:ID="Default" ss:Name="Normal ">
<Alignment ss:Vertical="Bo ttom" />
<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(p ath);
XPathNavigator xpNav = xpDoc.CreateNav igator();
XPathNodeIterat or xpite =
xpNav.Select("/Workbook/Styles/Style/@ID");
Console.WriteLi ne("No of item::"+xpite.C ount.ToString() );
while(xpite.Mov eNext())
{
Console.WriteLi ne(xpite.Curren t.Value);
}

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

Xpath:System.Xm l.XmlException: This is an unexpected token. The
expected token i
s 'NAME'. Line 14, position 15.
at System.Xml.XmlT extReader.SetAt tributeValues()
at System.Xml.XmlT extReader.Parse Element()
at System.Xml.XmlT extReader.Read( )
at System.Xml.XmlV alidatingReader .ReadNoCollectT extToken()
at System.Xml.XmlV alidatingReader .Read()
at System.Xml.XPat h.XPathDocument .ReadChildNodes (XPathContainer
parent, Strin
g parentBaseUri, XmlReader reader, PositionInfo positionInfo)
at System.Xml.XPat h.XPathDocument .ReadChildNodes (XPathContainer
parent, Strin
g parentBaseUri, XmlReader reader, PositionInfo positionInfo)
at System.Xml.XPat h.XPathDocument .ReadChildNodes (XPathContainer
parent, Strin
g parentBaseUri, XmlReader reader, PositionInfo positionInfo)
at System.Xml.XPat h.XPathDocument .ReadChildNodes (XPathContainer
parent, Strin
g parentBaseUri, XmlReader reader, PositionInfo positionInfo)
at System.Xml.XPat h.XPathDocument .Load(XmlReader reader)
at System.Xml.XPat h.XPathDocument .Init(XmlReader reader)
at System.Xml.XPat h.XPathDocument ..ctor(String uri)
at test.Class1.cel lDiff(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="Botto m" />
<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 2111
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.S heet"?>
<Workbook xmlns="urn:sche mas-microsoft-com:office:spre adsheet"
xmlns:o="urn:sc hemas-microsoft-com:office:offi ce"
xmlns:x="urn:sc hemas-microsoft-com:office:exce l"
xmlns:ss="urn:s chemas-microsoft-com:office:spre adsheet"
xmlns:html="htt p://www.w3.org/TR/REC-html40">
<Styles>
<Style ss:ID="Default" ss:Name="Normal ">
<Alignment ss:Vertical="Bo ttom" />
<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_4 188_8fd2_bcc24b efeac6"
ss:RefersTo="=S heet1!R1C1:R4C3 " />
<NamedRange ss:Name="Reify_ 09021612_5a3c_4 188_8fd2_bcc24b efeac6.C0"
ss:RefersTo="=S heet1!R1C1:R4C1 " />
<NamedRange ss:Name="Reify_ 09021612_5a3c_4 188_8fd2_bcc24b efeac6.C1"
ss:RefersTo="=S heet1!R1C2:R4C2 " />
<NamedRange ss:Name="Reify_ 09021612_5a3c_4 188_8fd2_bcc24b efeac6.C2"
ss:RefersTo="=S heet1!R1C3:R4C3 " />
<NamedRange ss:Name="Reify_ 09021612_5a3c_4 188_8fd2_bcc24b efeac6.R0"
ss:RefersTo="=S heet1!R1C1:R1C3 " />
<NamedRange ss:Name="Reify_ 09021612_5a3c_4 188_8fd2_bcc24b efeac6.R1"
ss:RefersTo="=S heet1!R2C1:R2C3 " />
<NamedRange ss:Name="Reify_ 09021612_5a3c_4 188_8fd2_bcc24b efeac6.R2"
ss:RefersTo="=S heet1!R3C1:R3C3 " />
<NamedRange ss:Name="Reify_ 09021612_5a3c_4 188_8fd2_bcc24b efeac6.R3"
ss:RefersTo="=S heet1!R4C1:R4C3 " />
<NamedRange ss:Name="Reify_ 4fd3e104_67ff_4 ac7_95a6_4a34dd 3251e1"
ss:RefersTo="=S heet1!R2C2:R3C3 " />
<NamedRange ss:Name="Reify_ 4fd3e104_67ff_4 ac7_95a6_4a34dd 3251e1.C0"
ss:RefersTo="=S heet1!R2C2:R3C2 " />
<NamedRange ss:Name="Reify_ 4fd3e104_67ff_4 ac7_95a6_4a34dd 3251e1.C1"
ss:RefersTo="=S heet1!R2C3:R3C3 " />
<NamedRange ss:Name="Reify_ 4fd3e104_67ff_4 ac7_95a6_4a34dd 3251e1.R0"
ss:RefersTo="=S heet1!R2C2:R2C3 " />
<NamedRange ss:Name="Reify_ 4fd3e104_67ff_4 ac7_95a6_4a34dd 3251e1.R1"
ss:RefersTo="=S heet1!R3C2:R3C3 " />
</Names>
<Table ss:ExpandedColu mnCount="3" ss:ExpandedRowC ount="4">
<Row>
<Cell ss:StyleID="s22 ">
<Data ss:Type="String ">v</Data>
<NamedCell ss:Name="Reify_ 09021612_5a3c_4 188_8fd2_bcc24b efeac6.R0"
/>
<NamedCell ss:Name="Reify_ 09021612_5a3c_4 188_8fd2_bcc24b efeac6.C0"
/>
<NamedCell ss:Name="Reify_ 09021612_5a3c_4 188_8fd2_bcc24b efeac6" />
</Cell>
</Row>
<Row>
<Cell ss:Index="2">
<Data ss:Type="String ">a</Data>
<NamedCell ss:Name="Reify_ 09021612_5a3c_4 188_8fd2_bcc24b efeac6.R1"
/>
<NamedCell ss:Name="Reify_ 4fd3e104_67ff_4 ac7_95a6_4a34dd 3251e1.R0"
/>
<NamedCell ss:Name="Reify_ 4fd3e104_67ff_4 ac7_95a6_4a34dd 3251e1.C0"
/>
<NamedCell ss:Name="Reify_ 4fd3e104_67ff_4 ac7_95a6_4a34dd 3251e1" />
<NamedCell ss:Name="Reify_ 09021612_5a3c_4 188_8fd2_bcc24b efeac6.C1"
/>
<NamedCell ss:Name="Reify_ 09021612_5a3c_4 188_8fd2_bcc24b efeac6" />
</Cell>
</Row>
<Row>
<Cell ss:Index="3">
<Data ss:Type="String ">a</Data>
<NamedCell ss:Name="Reify_ 4fd3e104_67ff_4 ac7_95a6_4a34dd 3251e1" />
<NamedCell ss:Name="Reify_ 4fd3e104_67ff_4 ac7_95a6_4a34dd 3251e1.C1"
/>
<NamedCell ss:Name="Reify_ 09021612_5a3c_4 188_8fd2_bcc24b efeac6.R2"
/>
<NamedCell ss:Name="Reify_ 4fd3e104_67ff_4 ac7_95a6_4a34dd 3251e1.R1"
/>
<NamedCell ss:Name="Reify_ 09021612_5a3c_4 188_8fd2_bcc24b efeac6" />
<NamedCell ss:Name="Reify_ 09021612_5a3c_4 188_8fd2_bcc24b efeac6.C2"
/>
</Cell>
</Row>
<Row>
<Cell ss:Index="3">
<Data ss:Type="String ">v</Data>
<NamedCell ss:Name="Reify_ 09021612_5a3c_4 188_8fd2_bcc24b efeac6" />
<NamedCell ss:Name="Reify_ 09021612_5a3c_4 188_8fd2_bcc24b efeac6.C2"
/>
<NamedCell ss:Name="Reify_ 09021612_5a3c_4 188_8fd2_bcc24b efeac6.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(p ath);
XPathNavigator xpNav = xpDoc.CreateNav igator();
XPathNodeIterat or xpite = xpNav.Select(xP ath);
Console.WriteLi ne("No of item::"+xpite.C ount.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:sche mas-microsoft-com:office:spre adsheet"

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 XmlNamespaceMan ager 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.CreateNav igator();

XmlNamespaceMan ager context = new
XmlNamespaceMan ager(xpNav.Name Table);

// Call dpx - TheDefaultPrefi x
context.AddName space("dpx","ur n:schemas-microsoft-com:office:spre adsheet");
context.AddName space("ss","urn :schemas-microsoft-com:office:spre adsheet");
context.AddName space("o","urn: schemas-microsoft-com:office:offi ce");
context.AddName space("x","urn: schemas-microsoft-com:office:exce l");

XPathExpression Expr = xpNav.Compile(x Path);
Expr.SetContext (context);

XPathNodeIterat or xpite = xpNav.Select(Ex pr);
Console.WriteLi ne("No of item::"+xpite.C ount.ToString() );

while(xpite.Mov eNext())
{
Console.WriteLi ne(xpite.Curren t.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
2589
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 see what happens, and socket.poll seems to behave very strangely. I've tried to use the .poll method for the poll object with and without a timeout, but in either case, the output randomly switches between on of the versions below. It runs fast,...
7
5479
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
3947
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. It is almost like it is trying to implement it's own COM interfaces... below is the header, and a link to the dll+code: Zip file with header, example, and DLL:...
0
1768
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 .TXT file with Keys/Values
7
2220
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 DB2's gimped BLOB API (sqludf_append() etc) look--to the extent that this is possible--like something C++ libraries can use to serialize stuff to (via iostream::read(), iostream::write() and what-not).
1
1818
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 "value" if entity="abc" and value="1" , and entity= "def" and value="2" at the Person/Category/Group/series/Key. My xml source file is as follows.
11
1936
by: aljaber | last post by:
hi, i am facing a problem with my program output here is the program /*********************************************\ * CD Database * * * \*********************************************/
5
15001
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 and why I have to do it. Hopefully someone has a suggestion... Alright, so I'm using a gps-simulation program that outputs gps data, like longitude, lattitude, altitude, etc. (hundreds of terms, these are just the well known ones). In the newer...
6
3537
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 to an old program that I wrote in delphi and it's a good opportunity to start with c++.
10
2211
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 has string value. I really do not understand why push_back() function is trying to remove previously inserted data. Thanks for any help
0
9855
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10908
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10587
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10649
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10295
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5682
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4487
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4064
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3136
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.