473,326 Members | 2,081 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,326 software developers and data experts.

Counting the number of records in a XML String

Hi,

I receive an XML string like
<record>
<value>...</value>
</record>
<record>
<value>...</value>
</record>....... etc.,

Now i want to find out how may records are there in that XML string, I do
not want to write anything to the drive is there any way to process that
string in the memory and get the number of records??

Thanks!
Nov 16 '05 #1
9 2969
hi
XmlDocument doc = new XmlDocument();
doc.LoadXML("myXmlstring");
XmlNodeList elemList = doc.GetElementsByTagName("record");

now elemList.Count will give the number of records

regards
Ansil

"GeRmIc" wrote:
Hi,

I receive an XML string like
<record>
<value>...</value>
</record>
<record>
<value>...</value>
</record>....... etc.,

Now i want to find out how may records are there in that XML string, I do
not want to write anything to the drive is there any way to process that
string in the memory and get the number of records??

Thanks!

Nov 16 '05 #2
Hi Ansil,

Thanks for your help, it works in other situations but when my XML string is
like this what do i do?? :(

<ns1:gName2 xmlns:ns1="http://abcd/gName2.xsd">
<ns1:ngid>763777</ns1:ngid>
<ns1:ntype>4</ns1:ntype>
</ns1:germplasmName2>
<ns2:gName2 xmlns:ns1="http://abcd/gName2.xsd">
<ns1:ngid>763777</ns1:ngid>
<ns1:ntype>4</ns1:ntype>
</ns2:germplasmName2> etc.,

This is actually marshalled by CASTOR and returned by a Java Web Service.

Thanks!
Nov 16 '05 #3
This isn't well formed XML, is this a direct cut and paste?

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<57**********************************@microsoft.co m>

Hi Ansil,

Thanks for your help, it works in other situations but when my XML string is
like this what do i do?? :(

<ns1:gName2 xmlns:ns1="http://abcd/gName2.xsd">
<ns1:ngid>763777</ns1:ngid>
<ns1:ntype>4</ns1:ntype>
</ns1:germplasmName2>
<ns2:gName2 xmlns:ns1="http://abcd/gName2.xsd">
<ns1:ngid>763777</ns1:ngid>
<ns1:ntype>4</ns1:ntype>
</ns2:germplasmName2> etc.,

This is actually marshalled by CASTOR and returned by a Java Web Service.

Thanks!

---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.770 / Virus Database: 517 - Release Date: 27/09/2004

[microsoft.public.dotnet.languages.csharp]
Nov 16 '05 #4
Actually I was trying to give an example of how the XML string looks.
Its returns a well formed XML. The mail body/records looks like this.

<ns1:gName2 xmlns:ns1="http://abcd/gName2.xsd">
<ns1:ngid>763777</ns1:ngid>
<ns1:ntype>4</ns1:ntype>
</ns1:gName2>
<ns2:gName2 xmlns:ns1="http://abcd/gName2.xsd">
<ns1:ngid>763777</ns1:ngid>
<ns1:ntype>4</ns1:ntype>
</ns2:gName2> etc.,

(NOTE : EXCUSE THE MISTAKE IN THE CLOSING TAG IN MY LAST POST!)
Nov 16 '05 #5
So the namespace is getting in the way?

Change the code that was posted to:

XmlNodeList elemList = doc.GetElementsByTagName("gName2", "http://abcd/gName2.xsd");

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<7F**********************************@microsoft.co m>

Actually I was trying to give an example of how the XML string looks.
Its returns a well formed XML. The mail body/records looks like this.

<ns1:gName2 xmlns:ns1="http://abcd/gName2.xsd">
<ns1:ngid>763777</ns1:ngid>
<ns1:ntype>4</ns1:ntype>
</ns1:gName2>
<ns2:gName2 xmlns:ns1="http://abcd/gName2.xsd">
<ns1:ngid>763777</ns1:ngid>
<ns1:ntype>4</ns1:ntype>
</ns2:gName2> etc.,

(NOTE : EXCUSE THE MISTAKE IN THE CLOSING TAG IN MY LAST POST!)

---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.770 / Virus Database: 517 - Release Date: 27/09/2004

[microsoft.public.dotnet.languages.csharp]
Nov 16 '05 #6
Thanks again Richard,

Its not the namespace, its the record count that is padded with the XML
string.

Like <ns1:gName2>... for the 1st record
<ns2:gName2>... for the 2nd record etc.,

I was able to replace every instance of ns1:, ns2:, ns3: etc with black and
now i am able to get the record count.

BUT!! HOW DO I KNOW HOW MANY "ns?" REPLACEMENTS I HAVE TO MAKE WITHOUT
KNOWING HOW MANY RECORDS ARE RETURNED :(

like replace ns1:, ns2:, ns3:... for how many???
Nov 16 '05 #7
Sorry, in the example you posted (that was well formed) both gName2 elements were in the same namespace just with different prefixes. Both ns1 and ns2 referred to the

http://abcd/gName2.xsd"

namespce. The prefix is irrelevent as far as XML processing goes its the actual namespace that matters - thats why the code I posted should work. If this is not the issue can you paste some of the actual XML as its getting quite frustrating coming up with solutions that don't work for you because the XML you keep posting doesn't match the actual situation you are facing

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<65**********************************@microsoft.co m>

Thanks again Richard,

Its not the namespace, its the record count that is padded with the XML
string.

Like <ns1:gName2>... for the 1st record
<ns2:gName2>... for the 2nd record etc.,

I was able to replace every instance of ns1:, ns2:, ns3: etc with black and
now i am able to get the record count.

BUT!! HOW DO I KNOW HOW MANY "ns?" REPLACEMENTS I HAVE TO MAKE WITHOUT
KNOWING HOW MANY RECORDS ARE RETURNED :(

like replace ns1:, ns2:, ns3:... for how many???

---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.770 / Virus Database: 517 - Release Date: 27/09/2004

[microsoft.public.dotnet.languages.csharp]
Nov 16 '05 #8
Hi Richard,

This is the XML file i am receiving, sometimes there are 4 records as in
this one, sometimes more than that.

My question is how do i find out the number of records returned in this case.

Thanks!

<?xml version="1.0" encoding="UTF-8" ?>
- <array-list xsi:type="java:java.util.ArrayList"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
- <ns1:germplasmName2 xmlns:ns1="http://germic.org/data/gms/gName2.xsd">
<ns1:ngid>763777</ns1:ngid>
<ns1:ntype>4</ns1:ntype>
<ns1:nstat>1</ns1:nstat>
</ns1:germplasmName2>
- <ns2:germplasmName2 xmlns:ns2="http://germic.org/data/gms/gName2.xsd">
<ns2:ngid>679645</ns2:ngid>
<ns2:ntype>6</ns2:ntype>
<ns2:nstat>0</ns2:nstat>
</ns2:germplasmName2>
- <ns3:germplasmName2 xmlns:ns3="http://germic.org/data/gms/gName2.xsd">
<ns3:ngid>679644</ns3:ngid>
<ns3:ntype>4</ns3:ntype>
<ns3:nstat>1</ns3:nstat>
</ns3:germplasmName2>
- <ns4:germplasmName2 xmlns:ns4="http://germic.org/data/gms/gName2.xsd">
<ns4:ngid>679643</ns4:ngid>
<ns4:ntype>4</ns4:ntype>
<ns4:nstat>1</ns4:nstat>
</ns4:germplasmName2>
</array-list>
Nov 16 '05 #9
XmlDocument doc = new XmlDocument();
doc.Load(@"c:\work\test.xml");
XmlNodeList elemList = doc.GetElementsByTagName("germplasmName2", "http://germic.org/data/gms/gName2.xsd");
Console.WriteLine(elemList.Count);

Gets you the number of records for this XML if its contained in a file called test.xml.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<4A**********************************@microsoft.co m>

Hi Richard,

This is the XML file i am receiving, sometimes there are 4 records as in
this one, sometimes more than that.

My question is how do i find out the number of records returned in this case.

Thanks!

<?xml version="1.0" encoding="UTF-8" ?>
- <array-list xsi:type="java:java.util.ArrayList"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
- <ns1:germplasmName2 xmlns:ns1="http://germic.org/data/gms/gName2.xsd">
<ns1:ngid>763777</ns1:ngid>
<ns1:ntype>4</ns1:ntype>
<ns1:nstat>1</ns1:nstat>
</ns1:germplasmName2>
- <ns2:germplasmName2 xmlns:ns2="http://germic.org/data/gms/gName2.xsd">
<ns2:ngid>679645</ns2:ngid>
<ns2:ntype>6</ns2:ntype>
<ns2:nstat>0</ns2:nstat>
</ns2:germplasmName2>
- <ns3:germplasmName2 xmlns:ns3="http://germic.org/data/gms/gName2.xsd">
<ns3:ngid>679644</ns3:ngid>
<ns3:ntype>4</ns3:ntype>
<ns3:nstat>1</ns3:nstat>
</ns3:germplasmName2>
- <ns4:germplasmName2 xmlns:ns4="http://germic.org/data/gms/gName2.xsd">
<ns4:ngid>679643</ns4:ngid>
<ns4:ntype>4</ns4:ntype>
<ns4:nstat>1</ns4:nstat>
</ns4:germplasmName2>
</array-list>

---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.770 / Virus Database: 517 - Release Date: 27/09/2004

[microsoft.public.dotnet.languages.csharp]
Nov 16 '05 #10

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

Similar topics

8
by: Brenda J. | last post by:
Hello all I'm sure this is a relatively simple query to create, but I can't seem to get my head around it. Here is an example of the two tables I am using: tblAccountTypes ...
5
by: chrisc | last post by:
Hello, Hope this is the right place for this... I am creating a testing database for components as they come off a production line. My reports need to select faults that are found, as well...
5
by: ChadDiesel | last post by:
Hello Again, I want to assign a number to each record that will be part of a shipping number. I want the number value to count up until the contract number changes. Then, I want the number to...
18
by: ChadDiesel | last post by:
I appreciate the help on this group. I know I've posted a lot here the last couple of weeks, but I was thrown into a database project at my work with very little Access experience. No other...
1
by: j | last post by:
Hi, I've been trying to do line/character counts on documents that are being uploaded. As well as the "counting" I also have to remove certain sections from the file. So, firstly I was working...
2
by: allyn44 | last post by:
Hello, I have built a serch form for users to edit records. I only want them to pull up the record they need, and I want to check for nulls. There should not be dupes becasue the underlying...
8
by: johnds | last post by:
This is my third question about eliminating accounting entries in a clinical database, yet retaining the valid record. Latest wrinkle is being able to sum the visits to the doctor, and the units of...
0
by: Chia Lee Lee | last post by:
Hello… I have problem when counting the number of records, which is based on the start date and end date. I have tried to use message box to prompt the result, but the result given is...
4
by: MLH | last post by:
MyString = "All men are created equal" Debug.PrintLen(MyString) Now that's easy. But how about just counting the letter "e"? Or, if I were curious to know how many commas were in the string....
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.