0

I have got an XML node called 'structNumber' which has data similar to '4.2'

I select the node using:

XmlNode xnChapNr = xDoc.SelectSingleNode("//./structNumber");

And at present I am displaying it by:

 string chapNr = (xnChapNr == null) ? "X" : xnChapNr.InnerText

This then displays the entire string '4.2'.

What I need however, is a way to only select the '4' for this string and the '2' for another.

Is there an extension for InnerText? I have read through the documentation but nothing appears to work as I wish it to.

If it is helpful to know; what this code does in its entirety is produce a tree structure of an XML document. I.e: Chapter 4, Section 4.1, Sub section 4.1.1, Section 4.2, ect...

Any and all help will be much appreciated.

mysterious-bob
  • 105
  • 2
  • 10
  • Doesn't `Split` do what you want? `string[] chapNrs = (xnChapNr ?? "").Split(".".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);` After this the array will either be empty or contain one or more chapter numbers. Then you can print 'X' if `.Length==0` or do whatever else it is you intend to do with the array – Andras Zoltan Oct 12 '11 at 09:32

2 Answers2

0

What you are looking for is not related to XML but to String operation. What you are looking for is the Split function http://msdn.microsoft.com/en-us/library/b873y76a.aspx

Vincent B.
  • 524
  • 3
  • 15
  • Although note it can be achieved with XSLT if he's happy altering the XML structure first: http://stackoverflow.com/questions/136500/does-xslt-have-a-split-function – Andras Zoltan Oct 12 '11 at 09:35
  • Thanks for the suggestion to use XSLT, I hadn't even considered that. However, in this case I think it is easier to just use Split like yourself and the others have suggested. – mysterious-bob Oct 12 '11 at 13:30
0

string chapNr = (xnChapNr == null) ? "X" : xnChapNr.InnerText.ToString().Split(new char[]{'.'})[0] will return you 4 and

string chapNr = (xnChapNr == null) ? "X" : xnChapNr.InnerText.ToString().Split(new char[]{'.'})[1] will return you 2

Haris Hasan
  • 29,856
  • 10
  • 92
  • 122