When trying to initialise an XDocument with a string containing XML, I kept getting the error:
Unhandled Exception: System.ArgumentException: Non white space characters cannot be added to content.
With help from a colleague, and recalling how I'd got around this error previously, the solution is to use the static XDocument.Parse( string ) method. Eg:
var myXDoc = XDocument.Parse( xmlString );
Showing posts with label Xml. Show all posts
Showing posts with label Xml. Show all posts
Friday, 14 January 2011
Thursday, 6 August 2009
Using Linq lambda expressions to retrieve XML elements based on the value of an attribute
The following example program loads an XML file, then selects all the elements named "field" within it. It then uses a lambda expression to get the inner text of the "alternativeTitle" element.
class Program
{
static void Main( string[] args )
{
XDocument xd = XDocument.Load( "..\\..\\file.xml" );
var fields = from el in xd.Descendants()
where el.Name.ToString().StartsWith( "field" )
select el;
Console.WriteLine( GetElementValue(fields, "alternativeTitle") );
Console.ReadLine();
}
static string GetElementValue( IEnumerable<XElement> elements, string attributeName )
{
return elements.Single( p => p.Attributes().Single( q => q.Name == "name" ).Value == attributeName ).Value;
}
}
The XML file used in the example is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<add>
<doc>
<field name="id">some_id</field>
<field name="readableid">interesting_publications</field>
<field name="name">Interesting Publications</field>
<field name="alternativeTitle">Online publications</field>
</doc>
</add>
The program outputs the text:
Online publications
class Program
{
static void Main( string[] args )
{
XDocument xd = XDocument.Load( "..\\..\\file.xml" );
var fields = from el in xd.Descendants()
where el.Name.ToString().StartsWith( "field" )
select el;
Console.WriteLine( GetElementValue(fields, "alternativeTitle") );
Console.ReadLine();
}
static string GetElementValue( IEnumerable<XElement> elements, string attributeName )
{
return elements.Single( p => p.Attributes().Single( q => q.Name == "name" ).Value == attributeName ).Value;
}
}
The XML file used in the example is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<add>
<doc>
<field name="id">some_id</field>
<field name="readableid">interesting_publications</field>
<field name="name">Interesting Publications</field>
<field name="alternativeTitle">Online publications</field>
</doc>
</add>
The program outputs the text:
Online publications
Wednesday, 26 March 2008
Cannot set a value on node type 'Element'
This error occurs if you wrongly use the Value property of XmlNode to change the value of an element.
Instead, use the InnerText property:
XmlDocument xdoc = new XmlDocument();
xdoc.Load( MapPath( "XmlSample.xml" ) );
XmlNode node =
xdoc.SelectSingleNode( "//element[@id=100]" );
// The following line will lead to
// an InvalidOperationException
node.Value = "New value!!";
xdoc.Save( MapPath( "XmlSample.xml" ) );
Instead, use the InnerText property:
node.InnerText = "New value!!";
[Edited: 4 Aug 2008]
Labels:
.NET,
Troubleshooting,
Xml
Tuesday, 25 March 2008
Validate XML in IE using JavaScript
Slightly off-topic, but as there don’t seem to be many good examples on the web of how to validate an XML document in IE using JavaScript, here’s a quick snippet. In the event of an XML error, an error message is displayed in the div.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>XML Validator</title>
<script type="text/javascript">
function loadit()
{
var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.validateOnParse = true;
xmlDoc.async="false";
xmlDoc.load("MyXmlFile.xml");
if (xmlDoc.parseError != '0')
{
document.getElementById("error").innerHTML
= xmlDoc.parseError.reason;
}
}
</script>
</head>
<body onload="loadit();">
<div id="error">
</div>
</body>
</html>
Subscribe to:
Posts (Atom)