tags:

views:

81

answers:

4

Could anybody please tell me how to extract the first 30 characters of the contents from XML file?

A: 

Open the file Ask the file stream reader to read in first 30 bytes Close the file

If you want the non-tag 30 characters, read the first 200 bytes, then run a regular expression to remove the tags.

Alex
reading the first 200 bytes doesn't work. It might very well contain only tags, leaving you with less than 30 characters ;^)
Toad
yeah yeah yeah. Ideally, get the first X chars, run the regex, if not enough, get more bytes.
Alex
+7  A: 

Open the file in notepad, and select the first 30 characters. Hit Ctrl-C.

If you want to do this programmatically, you'll need to tell us what language you're using.

Charlie Salts
I wanted to write that myself, but hadn't had the guts :-(
ApoY2k
I'm tired and I don't give a sh*t. Ask a silly question - get a silly answer :)
Charlie Salts
Reviewing my comment, `hadn't had` - is that even correct oO? Too early to speak english *yawn*...
ApoY2k
@ ApoY2k - It's a little awkward. `did not have` is better.
Charlie Salts
A: 

On linux/unix/cygwin:

head -c 30 myfile.xml

If you want the first 30 text characters outside of the tags then:

1) install xmltwig - this is a perl module, so you will need to install perl if you do not have it. Xmltwig includes the xml_grep utility.

2) run:

xml_grep --text_only myfile.xml | head -c 30
Dave Kirby
+1  A: 

In C#, after reading the XML into an XmlDocument:

string s = doc.DocumentElement.InnerText.Substring(0, 30);

This returns the first 30 characters of the text nodes in the document, e.g.:

<foo>This is <bar>some sort of <baz>crazy</baz> markup.</bar></foo>

will return:

This is some sort of crazy mar
Robert Rossney