473,396 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Can someone help me to understand schema locations?

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!
Oct 27 '08 #1
4 2324
SeanInSeattle wrote:
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/
Oct 27 '08 #2
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:
SeanInSeattle wrote:
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/
Oct 27 '08 #3
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" <Se***********@discussions.microsoft.comwrote in message
news:4E**********************************@microsof t.com...
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:
>SeanInSeattle wrote:
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/

Oct 28 '08 #4
SeanInSeattle wrote:
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#.
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
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/
Oct 28 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Atif | last post by:
Hi I have read about XML and its use for transferring data. I have alot of mis conceptions. Let's take an example I have database at two locations L1 and L2, both locations are using different...
4
by: Ian | last post by:
I would like to set a path to a schema where both the xml file and the schema are on my local hard drive (e.g. c:\XML\auto.xml and c:\XML\auto.xsd) Thank you, Ian
2
by: Mike:o | last post by:
I need to validate XML documents (orders) against their schema before processing. The PurchaseOrder schema that we use references 3 other schemas using the <imports ...> element. Here is the...
6
by: clvrmnky | last post by:
I've got a database backup from another system which I'd like to restore to a test system. Both the backup and restore systems are Solaris 8. I don't recall the DB2 version, but I do know that...
20
by: nicolas.riesch | last post by:
I try to understand strict aliasing rules that are in the C Standard. As gcc applies these rules by default, I just want to be sure to understand fully this issue. For questions (1), (2) and...
3
by: Tobias.Bischof | last post by:
Hi, i'm using xerces2 to deal with XML files my JAVA app stores data in. Now i have my schema located in %program-path%/resources but want to be free to store my XML files wherever i want and...
10
by: gmocnik | last post by:
I am validating XML files on a server which has no internet access and the validadation in C# does not work. Schema with which I am validating has namespaces like:...
2
by: Jiho Han | last post by:
I am trying to validate a document against a set of schema and in order to do that, I am using XmlValidatingReader.Schemas.Add to add the schemas. However, since there are dependencies, the...
1
by: =?Utf-8?B?RmlsaXBwbyBCZXR0aW5hZ2xpbw==?= | last post by:
I am trying to use a web service that publishes a WSDL with external schema (.XSD files), unfortunately the .NET wsdl.exe is not able to generate the proxy class for it. It seems that wsdl.exe...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.