Groups | Blog | Home
all groups > dotnet xml > december 2005 >

dotnet xml : Newbie in XML


Curious
12/24/2005 1:34:02 AM
Hi,

I am using the following code, and the xml being generated is shown
below:

XmlNode root = Rdf.document.DocumentElement;
XmlElement element = Rdf.document.CreateElement("rdf", "Stadium",
Rdf.RDF_PATH);
element.SetAttribute("about", null, Rdf.STADIUM_PATH+"/"+stadium.Name);
root.AppendChild(element);

<rdf:Stadium about="http://www.soccermanager.fake/stadium/test" />


Now, what I require is to generate the following xml:
<rdf:Stadium rdf:about="http://www.soccermanager.fake/stadium/test" />


How can this be done.
Can someone help me out.
Thanks in Advance
Curious
12/24/2005 6:23:52 AM
That problem was solved.

Now I am encoutering another problem.

I have the following xml file, and I require to retrieve all the values
for the <stadium:name> tag.

I have done this earlier, but with no namespaces. The code I was using
was the following:

StreamReader reader = new StreamReader(fileName, Encoding.ASCII);
XPathDocument doc = new XPathDocument(reader);
XPathNavigator nav = doc.CreateNavigator();
XPathExpression xpe = nav.Compile("RDF/Description/name");
XPathNodeIterator xpni = nav.Select(xpe);

while(xpni.MoveNext())
{
.....
}

How can this be applied having namespaces.
Thanks in Advance

<?xml version="1.0" encoding="us-ascii" standalone="no"?>
<rdf:RDF xmlns:player="http://www.soccermanager.fake/player#"
xmlns:club="http://www.soccermanager.fake/club#"
xmlns:stadium="http://www.soccermanager.fake/stadium#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<rdf:Description
rdf:about="http://www.soccermanager.fake/stadium/Giuseppe Meazza di San
Siro">
<stadium:underSoilSeating>True</stadium:underSoilSeating>
<stadium:capacity>85440</stadium:capacity>
<stadium:location>Milan</stadium:location>
<stadium:covered>False</stadium:covered>
<stadium:seated>85000</stadium:seated>
<stadium:name>Giuseppe Meazza di San Siro</stadium:name>
</rdf:Description>
<rdf:Description
rdf:about="http://www.soccermanager.fake/stadium/Delle Alpi">
<stadium:underSoilSeating>True</stadium:underSoilSeating>
<stadium:capacity>81000</stadium:capacity>
<stadium:location>Torino</stadium:location>
<stadium:covered>False</stadium:covered>
<stadium:seated>79000</stadium:seated>
<stadium:name>Delle Alpi</stadium:name>
</rdf:Description>
</rdf:RDF>
Martin Honnen
12/24/2005 1:41:49 PM


[quoted text, click to view]

The rdf prefix needs to be bound to a namespace URI, for instance
http://www.w3.org/1999/02/22-rdf-syntax-ns#
then you could code it as follows e.g.

string rdfNSURI = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
XmlDocument xmlDocument = new XmlDocument();
XmlElement stadium = xmlDocument.CreateElement("rdf:stadium",
rdfNSURI);
XmlAttribute about = xmlDocument.CreateAttribute("rdf", "about",
rdfNSURI);
about.Value = "http://www.soccermanager.fake/stadium/test";
stadium.SetAttributeNode(about);
Console.WriteLine(stadium.OuterXml);

and the serialization will be

<rdf:stadium rdf:about="http://www.soccermanager.fake/stadium/test"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" />

It needs to contain the xmlns:rdf declaration, at least as long as there
is no parent node that already defines that.


--

Martin Honnen --- MVP XML
Martin Honnen
12/24/2005 4:29:31 PM


[quoted text, click to view]


[quoted text, click to view]

What do you want to use, .NET 1.x or .NET 2.0?

With .NET 1.x you need to create an XmlNamespaceManager, add bindings of
prefixes to namespace URIs and call SetContext on the XPathExpression
object before you call Select, see
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemXmlXPathXPathExpressionClassSetContextTopic.asp>
Of course the XPath expression then needs to use qualified names with
the prefixes defined in the XmlNamespaceManager.

With .NET 2.0 you could have less work and use the navigator itself as
an IXmlNamespaceManager.

--

Martin Honnen --- MVP XML
Martin Honnen
12/24/2005 5:57:37 PM


[quoted text, click to view]

Here is an example, you need to move the navigator first to the root
element node to make sure the prefixes defined there are in scope:

XPathDocument xmlDocument = new XPathDocument(args[0]);
XPathNavigator xPathNavigator = xmlDocument.CreateNavigator();
xPathNavigator.MoveToChild(XPathNodeType.Element);


XPathNodeIterator nodeIterator =
xPathNavigator.Select("/rdf:RDF/rdf:Description/stadium:name",
xPathNavigator);

while (nodeIterator.MoveNext()) {
Console.WriteLine(nodeIterator.Current.Value);
}


--

Martin Honnen --- MVP XML
Curious
12/26/2005 12:27:07 AM
Hi,

I am using .NET 1.1. After following the link, still no data is being
given. The coding I am using is the following.

Is the query wrong, or something else.
Can some further help be given
Thanks in Advance

StreamReader reader = new StreamReader("c:\\MyRDF.rdf",
Encoding.ASCII);

XPathDocument doc = new XPathDocument(reader);
XPathNavigator nav = doc.CreateNavigator();

XmlNamespaceManager xnm = new XmlNamespaceManager(nav.NameTable);
xnm.AddNamespace("rdf", "http://www.soccermanager.fake/player#");
xnm.AddNamespace("stadium", "http://www.soccermanager.fake/stadium#");
xnm.AddNamespace("club", "http://www.soccermanager.fake/club#");
xnm.AddNamespace("player", "http://www.soccermanager.fake/player#");

XPathExpression xpe =
nav.Compile("/rdf:RDF/rdf:Description/stadium:name");
xpe.SetContext(xnm);
XPathNodeIterator xpni = nav.Select(xpe);

while(xpni.MoveNext())
{
Console.WriteLine(xpni.Current.Value.ToString());
}
Curious
12/26/2005 12:43:45 AM
Problem was solved. I had on wrong namespace, and also rearranged the
query.

Thanks for your help

XmlNamespaceManager xnm = new XmlNamespaceManager(nav.NameTable);
xnm.AddNamespace("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
xnm.AddNamespace("stadium", "http://www.soccermanager.fake/stadium#");
xnm.AddNamespace("club", "http://www.soccermanager.fake/club#");
xnm.AddNamespace("player", "http://www.soccermanager.fake/player#");


XPathExpression xpe =
nav.Compile("//rdf:RDF//rdf:Description//stadium:name");
AddThis Social Bookmark Button