Link to home
Start Free TrialLog in
Avatar of jimbona27
jimbona27Flag for United Kingdom of Great Britain and Northern Ireland

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</testdata1>
  <testdata2>today</testdata2>
  <testdata3>monday</testdata3>


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)
            {
            }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of kruegerste
kruegerste
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jimbona27

ASKER

I've tried this although I get the object not set runtime error.

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          
           

Open in new window

You need to use the prefixes of the namespaces created, as such:

("//apd:testdata1", manager) or just ("//apd:testdata1").InnerText
how do you know its prefixed with "apd" and not one of the other ones?  i.e. con, xsi etc...

thanks,
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 = "";

Open in new window

i can get it working using this code instead????

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...

Open in new window

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/apd:DC.Date.Modified
<?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-19T00:00:00</DC.Date.Created>
  <DC.Date.Issued>2008-12-28T09:17:51.370</DC.Date.Issued>
  <DC.Date.Modified>2009-02-02T09</DC.Date.Modified>
...
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.Modified");

                foreach (XmlNode Xnode1 in xList)
                {
                    DCDateModified = Xnode1.InnerText;
                }

                xList = xDoc.GetElementsByTagName("DC.Identifier");
                foreach (XmlNode Xnode2 in xList)
                {
                    DCIdentifier = Xnode2.InnerText;
                }



i.e.

string DCIdentifier = string.empty;
DCIdentifier = (XmlNode)xList[0].InnerText;  // does not work??

is this logically better or ugly?