Archive for the 'xml' Category

17
Jan
12

easy xml parsing in c#

Just a simple usage example:


using System.Xml;
...
XmlDocument doc = new XmlDocument();
// loading a file
doc.Load("file.xml");

// searching for multiple nodes via XPath; this will select all  elements wherever they may be
XmlNodeList books = doc.SelectNodes("//books");

foreach(XmlNode node in books)
{
   // getting attributes; assuming <book Author="Whatever">
   XmlAttribute author = node.Attributes["Author"];
   string value = author.Value;
}

Advertisements
30
Sep
11

validate an xml using xmllint and a xsd


Here’s how:


xmllint.exe --schema some_schema.xsd some.xml

03
Sep
11

xml content not allowed in prolog


If you get this error:


build.xml:2: Content is not allowed in prolog

The first thing to check is that you don’t have anything before:


<?xml version="1.0" encoding="utf-8"?>

The second, check that you don’t have a strange encoding. For example, one of the reasons it failed for me, was that build.xml was encoded using UCS2 Little Endian.

03
Sep
11

nokogiri strict parsing


By default Nokogiri is very forgiving. But, it’s forgiveness can be a source of bugs, as I experienced first-hand today. Here’s how you process an XML in a strict way:


Nokogiri::XML( File.read( file ) ) do |config|
   config.strict
end

09
Aug
11

XPath with REXML


Here’s how you search by XPath in REXML:


XPath.each(node,"//*[@WhateverAttribute]") do |found_element|

end

11
Jan
10

SAXException: Invalid byte 2 of 2-byte UTF-8 sequence


If you ever receive this exception: SAXException: Invalid byte 2 of 2-byte UTF-8 sequence, then it means that the XML file you’re trying to read wasn’t encoded in UTF-8. If by any chance,you’re creating that XML, make sure you use the proper encoding when writing the file ( in this case UTF-8 ).

15
Oct
09

getting a basename from a path using ant


If you have a path written in a file, and you’d like to get a basename from it, like in the following example:

/this/is/my/path.txt

after you run this target, you will have only path.txt. Here’s the code that makes that possible:


<target name="rgx" description="basename equivalent">
	<replaceregexp file="a.txt" match="(.*/)|(.*\\)" replace="" byline="true"/>
</target>




Blog Stats

  • 229,299 hits