jimbona27
asked on
rookie problem with xml namespaces in c#
Hi EE,
I need to add multiple namespaces for Xpath a document. I recently closed a question for one namespace but Im
not sure what happens when I need to add multiple namespaces.
<?xml version="1.0" encoding="utf-8"?>
<test xsi:schemaLocation="xxx" xmlns="xxx" xmlns:apd="xxx" xmlns:bs7666="xxx" xmlns:xsi="xxx" xmlns:con="xxx">
<testdata1>example</testda ta1>
<testdata2>today</testdata 2>
<testdata3>monday</testdat a3>
any ideas how I can add multiple namespaces using c# so I can iterate through XmlNodeList?
Many thanks,
I need to add multiple namespaces for Xpath a document. I recently closed a question for one namespace but Im
not sure what happens when I need to add multiple namespaces.
<?xml version="1.0" encoding="utf-8"?>
<test xsi:schemaLocation="xxx" xmlns="xxx" xmlns:apd="xxx" xmlns:bs7666="xxx" xmlns:xsi="xxx" xmlns:con="xxx">
<testdata1>example</testda
<testdata2>today</testdata
<testdata3>monday</testdat
any ideas how I can add multiple namespaces using c# so I can iterate through XmlNodeList?
Many thanks,
XmlDocument xmldoc = new XmlDocument();
XmlNamespaceManager nsMgr = new XmlNamespaceManager(xmldoc.NameTable);
nsMgr.AddNamespace("xmlns:apd", "ass");
nsMgr.AddNamespace("??", "xxxx");
nsMgr.AddNamespace("??", "xxxx");
nsMgr.AddNamespace("??", "xxxx");
xmldoc.LoadXml(xstring);
XmlNodeList nodeList = xmldoc.SelectNodes("??", nsMgr);
foreach (XmlNode Xnode in nodeList)
{
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
https://www.experts-exchange.com/questions/21860351/Multiple-Namespaces-XPath-with-C.html?sfQueryTermInfo=1+multipl+namespac
ASKER
I've tried this although I get the object not set runtime error.
any reason why this error would happen?
any reason why this error would happen?
string test = string.Empty;
XmlDocument _XML = new XmlDocument();
_XML.LoadXml(xstring);
XmlNamespaceManager manager = new XmlNamespaceManager(_XML.NameTable);
manager.AddNamespace("bs7666", "http://www.govtalk.gov.uk/people/bs7666");
manager.AddNamespace("apd", "xxx");
manager.AddNamespace("con", "xxx");
manager.AddNamespace("xsi", "xxx");
test = _XML.SelectSingleNode("//theroot/myfield", manager).InnerText; // errors on this line
You need to use the prefixes of the namespaces created, as such:
("//apd:testdata1", manager) or just ("//apd:testdata1").InnerT ext
("//apd:testdata1", manager) or just ("//apd:testdata1").InnerT
ASKER
how do you know its prefixed with "apd" and not one of the other ones? i.e. con, xsi etc...
thanks,
thanks,
ASKER
using this example, i still get the object is not set.
string test = string.Empty;
XmlDocument _XML = new XmlDocument();
_XML.LoadXml(xstring);
XmlNamespaceManager manager = new XmlNamespaceManager(_XML.NameTable);
manager.AddNamespace("xx", "http://www.govtalk.gov.uk/people/bs7666");
manager.AddNamespace("apd", "http://www.govtalk.gov.uk/people/AddressAndPersonalDetails");
manager.AddNamespace("con", "http://www.govtalk.gov.uk/people/ContactTypes");
manager.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
test = _XML.SelectSingleNode("//apd:ServiceDescription/DC.Date.Modified", manager).InnerText;
test = "";
ASKER
i can get it working using this code instead????
so why use the selectsinglenode if this other code works?
so why use the selectsinglenode if this other code works?
//Xmlvalue = xDoc.SelectSingleNode("//apd:ServiceDescription/DC.Date.Modified", mngr).InnerText;
XmlNodeList test = xDoc.GetElementsByTagName("DC.Date.Modified");
foreach (XmlNode Xnode in test)
{
Xmlvalue = Xnode.InnerText;
}
//Xmlvalue holds the date value that I want...
Yeah, if that works, then use it. There are different ways to do any one thing. Here it depends on your Xml markup and expected results which I don't know of.
Could you provide the xml.
I would guess DC.Date.Modified is within a namespace as well. Probably also apd:
//apd:ServiceDescription/a pd:DC.Date .Modified
I would guess DC.Date.Modified is within a namespace as well. Probably also apd:
//apd:ServiceDescription/a
ASKER
<?xml version="1.0" encoding="utf-8"?>
<ServiceDescription xsi:schemaLocation="http://dcsf.gov.uk/XMLSchema/ServiceDirectory ServiceTypes-v1-3c.xsd" xmlns="http://dcsf.gov.uk/XMLSchema/ServiceDirectory" xmlns:apd="http://www.govtalk.gov.uk/people/AddressAndPersonalDetails" xmlns:bs7666="http://www.govtalk.gov.uk/people/bs7666" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:con="http://www.govtalk.gov.uk/people/ContactTypes">
<DC.Contributor Href="href text" Role="role text">contributor text</DC.Contributor>
<DC.Creator>DCCreator</DC. Creator>
<DC.Date.Created>2001-10-1 9T00:00:00 </DC.Date. Created>
<DC.Date.Issued>2008-12-28 T09:17:51. 370</DC.Da te.Issued>
<DC.Date.Modified>2009-02- 02T09</DC. Date.Modif ied>
...
<ServiceDescription xsi:schemaLocation="http://dcsf.gov.uk/XMLSchema/ServiceDirectory ServiceTypes-v1-3c.xsd" xmlns="http://dcsf.gov.uk/XMLSchema/ServiceDirectory" xmlns:apd="http://www.govtalk.gov.uk/people/AddressAndPersonalDetails" xmlns:bs7666="http://www.govtalk.gov.uk/people/bs7666" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:con="http://www.govtalk.gov.uk/people/ContactTypes">
<DC.Contributor Href="href text" Role="role text">contributor text</DC.Contributor>
<DC.Creator>DCCreator</DC.
<DC.Date.Created>2001-10-1
<DC.Date.Issued>2008-12-28
<DC.Date.Modified>2009-02-
...
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
im using this code although i know it only returns one for DC.Date.Modified and one for DC.Identifier, seems
stupid to be in a foreach, is there a way to get it out?
XmlNodeList xList = xDoc.GetElementsByTagName( "DC.Date.M odified");
foreach (XmlNode Xnode1 in xList)
{
DCDateModified = Xnode1.InnerText;
}
xList = xDoc.GetElementsByTagName( "DC.Identi fier");
foreach (XmlNode Xnode2 in xList)
{
DCIdentifier = Xnode2.InnerText;
}
i.e.
string DCIdentifier = string.empty;
DCIdentifier = (XmlNode)xList[0].InnerTex t; // does not work??
is this logically better or ugly?
stupid to be in a foreach, is there a way to get it out?
XmlNodeList xList = xDoc.GetElementsByTagName(
foreach (XmlNode Xnode1 in xList)
{
DCDateModified = Xnode1.InnerText;
}
xList = xDoc.GetElementsByTagName(
foreach (XmlNode Xnode2 in xList)
{
DCIdentifier = Xnode2.InnerText;
}
i.e.
string DCIdentifier = string.empty;
DCIdentifier = (XmlNode)xList[0].InnerTex
is this logically better or ugly?