Problem in Xml

  • Thread starter Thread starter Curious
  • Start date Start date
C

Curious

Hi,

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

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


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


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


How can this be done.
Can someone help me out.
Thanks in Advance
 
Curious said:
Hi,

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

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

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

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

In that case, make the second argument to SetAttribute RDF_PATH instead
of null. I believe that should do it...
 
That solved my problem.

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>
 
Curious,

What you need to do is pass an implementation of IXmlNamespaceResolver
to the overload of the Select method. This implementation would have
namespaces associated with the tags associated with them.

Generally, to do this, you would create an instance of
XmlNamespaceManager and then pass that in with the call to Select.

Hope this helps.
 
Back
Top