angus_young_acdc
asked on
Reading BizTalk XML Document?
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 :)
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 :)
ASKER
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.FullNam e);
XmlNamespaceManager man = new XmlNamespaceManager(docume nt.NameTab le);
man.AddNamespace("ns0", "ns0:Batch");
XmlNodeList list = document.SelectNodes("//ns 0:Batch/Do cument"); // Exception thrown here
XmlNode singleNode;
for (int i = 0; i < list.Count; i++)
{
singleNode = list[i];
string destination = singleNode.Attributes.GetN amedItem(" Destinatio n").Value. ToString() ;
}
}
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.FullNam
XmlNamespaceManager man = new XmlNamespaceManager(docume
man.AddNamespace("ns0", "ns0:Batch");
XmlNodeList list = document.SelectNodes("//ns
XmlNode singleNode;
for (int i = 0; i < list.Count; i++)
{
singleNode = list[i];
string destination = singleNode.Attributes.GetN
}
}
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:
- 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.
- 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("//ns
0:Batch/Do cument", man)
ASKER
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?
Would that mean that http://testSchema.Message is the namespace?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Cheers mate for the help and indeed the explanation :)
You're welcome, glad I could be of help :)
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 --