473,396 Members | 1,713 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,396 software developers and data experts.

How to count number of lines in XML using C#

Hi,
Below is the response XML my programmer is getting from business service -

Expand|Select|Wrap|Line Numbers
  1. <Data>
  2. <Maintain>
  3.   <AssetList>
  4.      <AssetDetails>
  5.           <AssetDescriptor> 
  6.              <ns1:ParentProductCat category="ABC">ABC</ns1:ParentProductCat
  7.           </AssetDescriptor>
  8.      </AssetDetails>
  9.      <AssetDetails>
  10.           <AssetDescriptor> 
  11.              <ns2:ParentProductCat category="DEF">DEF</ns2:ParentProductCat
  12.           </AssetDescriptor>
  13.      </AssetDetails>
  14.      <AssetDetails>
  15.           <AssetDescriptor> 
  16.              <ns3:ParentProductCat category="GHI">GHI</ns3:ParentProductCat
  17.           </AssetDescriptor>
  18.      </AssetDetails>
  19.      <AssetDetails>
  20.           <AssetDescriptor> 
  21.              <ns4:ParentProductCat category="JKL">JKL</ns4:ParentProductCat
  22.           </AssetDescriptor>
  23.      </AssetDetails>
  24.      <AssetDetails>
  25.           <AssetDescriptor> 
  26.              <ns5:ParentProductCat category="MNP">MNP</ns5:ParentProductCat
  27.           </AssetDescriptor>
  28.      </AssetDetails>
  29.      <AssetDetails>
  30.           <AssetDescriptor> 
  31.              <ns6:ParentProductCat category="PQR">PQR</ns6:ParentProductCat
  32.           </AssetDescriptor>
  33.      </AssetDetails>
  34.      <AssetDetails>
  35.           <AssetDescriptor> 
  36.              <ns7:ParentProductCat category="YES">YES</ns7:ParentProductCat
  37.           </AssetDescriptor>
  38.      </AssetDetails>
  39.      <AssetDetails>
  40.           <AssetDescriptor> 
  41.              <ns8:ParentProductCat category="col">col</ns8:ParentProductCat
  42.           </AssetDescriptor>
  43.      </AssetDetails>
  44.   </AssetList>
  45. </Maintain>
  46. </Data>
In above XMl if you check the ParentProductCat is prefixed with ns1:,ns2:,ns3:....
My XSLT code only checkes for the ParentProductCat but since it is prefixed with "ns*:" (* is integer) it fails to find it. So that in C# code replace function is used to replace ns*: a for loop is used for this and it goes until 1000

But now my response crossed the 1000 mark and this time the XML contains total 14500 lines.
So I want to replace all ns:*

For this I want to count the number of lines present in the XML so that the for loop will run until that line number.

Please let me know how to do that?
Feb 4 '13 #1

✓ answered by Mikkeee

Is there any reason you have 1500 different namespaces? Also, your sample is missing the ending '>' tag on all your namespace lines. I'm assuming that happened when you pasted your code but you might want to take a look to be sure.

Write a single line of code to replace all ns* text using Regular Expressions:
string result = Regex.Replace(xmldata, @"ns\d{0,5}\:","ns:");

You could use the technique above to easily change all the tags but you might want to have the source clean up their xml.

Good luck!
Mike

2 1885
Rabbit
12,516 Expert Mod 8TB
There's no need to know how many lines there are beforehand. Just have the code read until it gets to the end of the file.
Feb 4 '13 #2
Mikkeee
94 64KB
Is there any reason you have 1500 different namespaces? Also, your sample is missing the ending '>' tag on all your namespace lines. I'm assuming that happened when you pasted your code but you might want to take a look to be sure.

Write a single line of code to replace all ns* text using Regular Expressions:
string result = Regex.Replace(xmldata, @"ns\d{0,5}\:","ns:");

You could use the technique above to easily change all the tags but you might want to have the source clean up their xml.

Good luck!
Mike
Feb 4 '13 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Anne | last post by:
Hi I have got a table with some sales transactions. It has got 3 fields 1) Ordernumber 2) Item number 3) Shippingdate Now I want to make ONE (not two) query that shows how many different...
0
by: DataFreakFromUtah | last post by:
Hello! No question here, just a procedure for the archive. Search critera: count records imported count data imported count number of rows imported count number of records imported record import...
4
by: tommcd24 | last post by:
Does anyone know of a utility or VS add-in to count code lines in a project or solution? I just had to provide 50 pages of code for copyright and had initially thought that would be nearly the...
4
by: Martin Hart | last post by:
Hi: Does anyone know of a utility to count the lines of code in a given solution? I'm using VS2005, just in case this makes any difference. TIA, MartinH.
11
by: Mack | last post by:
Hi all, I want to write a program to count number of bits set in a number. The condition is we should not loop through each bit to find whether its set or not. Thanks in advance, -Mukesh
2
by: mfaisalwarraich | last post by:
Hi Everybody, I am using the following code to get the recordset of an external database. Dim dbPatients As Database Dim rsCountPatients As Recordset ' to count number of...
5
by: jambonjamasb | last post by:
I am wanting to create a report that summarises the number of items within a date range. For example I have a FIELD called System_Change. This is a drop down COMBOBOX that uses words like unix,...
1
by: jlt206 | last post by:
This code <?php include("counter.php")?> on the webpage produces the count number. (function code below) I want to place the current number into a variable $MemberNo or into a FormField to be sent...
2
by: DexterID | last post by:
Hi, I tried so much working with this to get distinct Words and their count. I'm using ASP.NET 2.0. And i also googled so much. I need HELP from anyone to get the required output. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.