?
Solved

Reading BizTalk XML Document?

Posted on 2009-06-29
8
Medium Priority
?
679 Views
Last Modified: 2013-11-07
I'm trying to read attributes from a biztalk XML document, but get the following exception:
Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function.

I'm assuming it's something because the main node says <ns0:Batch ....>, but I'm really stumped as to how to actually do this.  Would appreciate a good explanation of what I have to do where possible as I prefer to learn rather than just read links :)
0
Comment
Question by:angus_young_acdc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 39

Expert Comment

by:abel
ID: 24740311
You posted in C#, questions on XML may receive better coverage in the XML zones... ;-)

It is usually not so hard to correct hat error, but it would be great to see a bit of code, esp. from Biztalk. If the namespaces are not bound, i.e., as a result of you copying and pasting bits of the XML, you are out of luck because then the XML has become invalid for normal XML processing. Normally, the namespace declarations are on top of the XML file.

-- Abel --
0
 
LVL 15

Author Comment

by:angus_young_acdc
ID: 24743354
I didn't post it in an XML zone because the error is coming from my .Net code, and I'm looking for the way that I can have C# read the XML ;)  

Well, more than happy to show code but considering I just assumed it was a matter of reading the XML like I normally do (which obviously not I find out isn't the case :( )

            XmlDocument document = new XmlDocument();
            DirectoryInfo info = new DirectoryInfo(location);
            FileInfo[] files = info.GetFiles("*.xml");
            foreach (FileInfo file in files)
            {
                document.Load(file.FullName);
                XmlNamespaceManager man = new XmlNamespaceManager(document.NameTable);
                man.AddNamespace("ns0", "ns0:Batch");
                XmlNodeList list = document.SelectNodes("//ns0:Batch/Document"); // Exception thrown here
                XmlNode singleNode;
                for (int i = 0; i < list.Count; i++)
                {
                    singleNode = list[i];
                    string destination = singleNode.Attributes.GetNamedItem("Destination").Value.ToString();
                }
            }
0
 
LVL 39

Expert Comment

by:abel
ID: 24743412
Thanks for posting the code. I actually hoped for some BizTalk code as well, because that's what contains the actual namespace. However, there are two things to note with your code:

  1. The AddNameSpace takes the namespace as the second argument, not the namespace plus prefix. I.e., for an Atom feed this would be nsMgr.AddNamespace("a", "http://www.w3.org/2005/Atom"). I need to see your real namespace (which is probably not "Batch") to advice further on this. Check the XML for that.
  2. The namespace manager does nothing if it is isn't used. The line with SelectNodes takes a second argument which can be the namespace manager. Change you line to document.SelectNodes("//ns0:Batch/Document", man)
-- Abel --
0
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
LVL 15

Author Comment

by:angus_young_acdc
ID: 24743595
Unfortunately I don't have any BizTalk code. :(   The following is a small example of the XML I'm forced to work with, not sure if it will be of any use:








Would that mean that http://testSchema.Message is the namespace?  
0
 
LVL 39

Accepted Solution

by:
abel earned 2000 total points
ID: 24743640
That's precisely what I wanted to see!

And you answered it yourself: the namespace is http://testSchema.Message, which you can use to add to the namespace manager.

-- Abel --
0
 
LVL 39

Assisted Solution

by:abel
abel earned 2000 total points
ID: 24743875
To make that complete, using a copy of your code (I didn't test this, but it should give you a hint on how it goes):

On a side note: you use ns0 here because it is the same as in the source XML document. For the XPath inside SelectNodes it is not necessary to use the same namespace prefix, you could call "ns0" "jan" or "message" or whatever, as long as the binding is exactly correct (literally: it is case sensitive and extra trailing slashes or whatever really means a different namespace).

-- Abel --

XmlDocument document = new XmlDocument();
DirectoryInfo info = new DirectoryInfo(location);            
FileInfo[] files = info.GetFiles("*.xml");            
foreach (FileInfo file in files)            
{            
    document.Load(file.FullName);            
    XmlNamespaceManager man = new XmlNamespaceManager(document.NameTable);            
    man.AddNamespace("ns0", "http://testSchema.Message");            
    XmlNodeList list = document.SelectNodes("//ns0:Batch/Document", man); 
    XmlNode singleNode;            
    for (int i = 0; i < list.Count; i++)            
    {            
        singleNode = list[i];            
        string destination = singleNode.Attributes.GetNamedItem("Destination").Value.ToString();
    }            
}            

Open in new window

0
 
LVL 15

Author Closing Comment

by:angus_young_acdc
ID: 31598000
Cheers mate for the help and indeed the explanation :)
0
 
LVL 39

Expert Comment

by:abel
ID: 24746445
You're welcome, glad I could be of help :)
0

Featured Post

Linux Academy Android App Now Supports Chromecast

We have some fantastic news for our Android fans. We’re so excited to announce that the Linux Academy Android app is now available with Chromecast support. That’s right – simply download the latest update of the Linux Academy App and start casting your favorite course videos!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question