Connecting Tech Pros Worldwide Help | Site Map

Can someone help me to understand schema locations?

  #1  
Old October 27th, 2008, 07:35 PM
=?Utf-8?B?U2VhbkluU2VhdHRsZQ==?=
Guest
 
Posts: n/a
I have this xml:
[xml]

<Report p1:schemaLocation="Data_Feed
http://server/reportserver?param1=foo&param2=bar"
Name="Data Feed"
xmlns:p1="http://www.w3.org/2001/XMLSchema-instance"
xmlns="Data_Feed">
<Data/>
</Report>

[/xml]

And, I'm trying to read it... without success. I'm using code from this
KB318545 (http://support.microsoft.com/kb/318545), but not having much luck.
That its not working is probably because I don't understand how to map the
info above to the parameters needed for the xmlDoc.SelectNodes(xpath) to
work.

Any help would be much appreciated!
  #2  
Old October 27th, 2008, 07:45 PM
Martin Honnen
Guest
 
Posts: n/a

re: Can someone help me to understand schema locations?


SeanInSeattle wrote:
Quote:
I have this xml:
[xml]
>
<Report p1:schemaLocation="Data_Feed
http://server/reportserver?param1=foo&param2=bar"
Name="Data Feed"
xmlns:p1="http://www.w3.org/2001/XMLSchema-instance"
xmlns="Data_Feed">
<Data/>
</Report>
>
[/xml]
>
And, I'm trying to read it... without success. I'm using code from this
KB318545 (http://support.microsoft.com/kb/318545), but not having much luck.
That its not working is probably because I don't understand how to map the
info above to the parameters needed for the xmlDoc.SelectNodes(xpath) to
work.
You need an XmlNamespaceManager
XmlDocument doc = new XmlDocument();
doc.Load("file.xml");

XmlNamespaceManager mgr = new XmlNamespaceManager(doc.NameTable);
mgr.AddNamespace("df", doc.DocumentElement.NamespaceURI);

foreach (XmlElement data in doc.SelectNodes("df:Report/df:Data", mgr))
{
...
}

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
  #3  
Old October 27th, 2008, 08:05 PM
=?Utf-8?B?U2VhbkluU2VhdHRsZQ==?=
Guest
 
Posts: n/a

re: Can someone help me to understand schema locations?


Ah, yes. I appreciate that. Though, which one is the URI in my example xml?
Is it the p1:schemalocation value altogether, or just the "http://[...]", or
is it the xmlns:p1 value?

Here's my code:

Expand|Select|Wrap|Line Numbers
  1. try {
  2. XmlDocument xDoc = new XmlDocument();
  3. xDoc.Load(XMLDoc_URL);
  4. System.Xml.XmlNamespaceManager xmlnsManager = new
  5. System.Xml.XmlNamespaceManager(xDoc.NameTable);
  6. xmlnsManager.AddNamespace(strDataFeedName, xDoc.DocumentElement.NamespaceURI);
  7. foreach (System.Xml.XmlNode xnDetail in
  8. xDoc.DocumentElement.SelectNodes(XPathToData, xmlnsManager))
  9. Console.WriteLine(xnDetail.Attributes.GetNamedItem(AttrName).Value);
  10. }catch (Exception e) { Console.WriteLine(e.Message); return -1; }
  11.  
"Martin Honnen" wrote:
Quote:
SeanInSeattle wrote:
Quote:
I have this xml:
[xml]

<Report p1:schemaLocation="Data_Feed
http://server/reportserver?param1=foo&param2=bar"
Name="Data Feed"
xmlns:p1="http://www.w3.org/2001/XMLSchema-instance"
xmlns="Data_Feed">
<Data/>
</Report>

[/xml]

And, I'm trying to read it... without success. I'm using code from this
KB318545 (http://support.microsoft.com/kb/318545), but not having much luck.
That its not working is probably because I don't understand how to map the
info above to the parameters needed for the xmlDoc.SelectNodes(xpath) to
work.
>
You need an XmlNamespaceManager
XmlDocument doc = new XmlDocument();
doc.Load("file.xml");
>
XmlNamespaceManager mgr = new XmlNamespaceManager(doc.NameTable);
mgr.AddNamespace("df", doc.DocumentElement.NamespaceURI);
>
foreach (XmlElement data in doc.SelectNodes("df:Report/df:Data", mgr))
{
...
}
>
--
>
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
>
  #4  
Old October 28th, 2008, 09:35 AM
Joe Fawcett
Guest
 
Posts: n/a

re: Can someone help me to understand schema locations?


There's no prefix on the Report element so it's in the default namespace.
The default namespace is indicated via an xmlns 'attribute' (with no prefix)
on the element or one of its parents. So in this case it's:
xmlns="Data_Feed"
That's what you'd see if you examined xDoc.DocumentElement.NamespaceURI in
the debugger.

The other namespace declaration has a prefix attached, xmlns:p1, so that
only pertains to nodes with a p1 prefix such as the p1:schemaLocation
attribute.

As an aside using p1 as a prefix is perfectly legal but a little unusual,
most examples using the schema instance namespace,
http://www.w3.org/2001/XMLSchema-instance, use xsi as the prefix.

--

Joe Fawcett (MVP - XML)

http://joe.fawcett.name

"SeanInSeattle" <SeanInSeattle@discussions.microsoft.comwrote in message
news:4EFC6F13-EA9D-4EF6-A512-120745D1CE28@microsoft.com...
Quote:
Ah, yes. I appreciate that. Though, which one is the URI in my example
xml?
Is it the p1:schemalocation value altogether, or just the "http://[...]",
or
is it the xmlns:p1 value?
>
Here's my code:
>
Expand|Select|Wrap|Line Numbers
  1. try {
  2. XmlDocument xDoc = new XmlDocument();
  3. xDoc.Load(XMLDoc_URL);
  4. System.Xml.XmlNamespaceManager xmlnsManager = new
  5. System.Xml.XmlNamespaceManager(xDoc.NameTable);
  6. xmlnsManager.AddNamespace(strDataFeedName,
  7. xDoc.DocumentElement.NamespaceURI);
  8. foreach (System.Xml.XmlNode xnDetail in
  9. xDoc.DocumentElement.SelectNodes(XPathToData, xmlnsManager))
  10. Console.WriteLine(xnDetail.Attributes.GetNamedItem(AttrName).Value);
  11. }catch (Exception e) { Console.WriteLine(e.Message); return -1; }
  12.  
>
"Martin Honnen" wrote:
>
Quote:
>SeanInSeattle wrote:
Quote:
I have this xml:
[xml]
>
<Report p1:schemaLocation="Data_Feed
http://server/reportserver?param1=foo&param2=bar"
Name="Data Feed"
xmlns:p1="http://www.w3.org/2001/XMLSchema-instance"
xmlns="Data_Feed">
<Data/>
</Report>
>
[/xml]
>
And, I'm trying to read it... without success. I'm using code from
this
KB318545 (http://support.microsoft.com/kb/318545), but not having much
luck.
That its not working is probably because I don't understand how to map
the
info above to the parameters needed for the xmlDoc.SelectNodes(xpath)
to
work.
>>
>You need an XmlNamespaceManager
> XmlDocument doc = new XmlDocument();
> doc.Load("file.xml");
>>
> XmlNamespaceManager mgr = new XmlNamespaceManager(doc.NameTable);
> mgr.AddNamespace("df", doc.DocumentElement.NamespaceURI);
>>
> foreach (XmlElement data in doc.SelectNodes("df:Report/df:Data", mgr))
> {
> ...
> }
>>
>--
>>
>Martin Honnen --- MVP XML
>http://JavaScript.FAQTs.com/
>>

  #5  
Old October 28th, 2008, 12:35 PM
Martin Honnen
Guest
 
Posts: n/a

re: Can someone help me to understand schema locations?


SeanInSeattle wrote:
Quote:
Ah, yes. I appreciate that. Though, which one is the URI in my example xml?
Is it the p1:schemalocation value altogether, or just the "http://[...]", or
is it the xmlns:p1 value?
Well the code I suggested simply takes the URI from the DocumentElement,
that is why I suggested to use
mgr.AddNamespace("df", doc.DocumentElement.NamespaceURI);
where "df" is a prefix you can freely choose and where the second
argument simply reads out the NamespaceURI from the DocumentElement. In
the sample you posted that URI is "Data_Feed" so assuming that is how
your real XML looks you could also use
mgr.AddNamespace("df", "Data_Feed");
but the code I posted works perfectly even when the XML changes while
the last suggestion would hard code the URI in the C#.
Quote:
Here's my code:
>
[code]
try {
XmlDocument xDoc = new XmlDocument();
xDoc.Load(XMLDoc_URL);
System.Xml.XmlNamespaceManager xmlnsManager = new
System.Xml.XmlNamespaceManager(xDoc.NameTable);
xmlnsManager.AddNamespace(strDataFeedName, xDoc.DocumentElement.NamespaceURI);
Without seeing the value of strDataFeedName
Quote:
foreach (System.Xml.XmlNode xnDetail in
xDoc.DocumentElement.SelectNodes(XPathToData, xmlnsManager))
and XPathToData it is not possible to tell whether your code works.





--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Closed Thread