c# - Null reference exception - XDocument using Element and XPath -


i getting xml data in form of string database. data saved ntext in database.

getting data database not problem. problem later, when want handle data in xml. load string xdocument.

i want owner value @ first. nullreference exception, meaning not write correct xpath assume.

writing "./owner" not work. writing "/./owner" not work xml exception.

i started out xmldocument think ran namespace problem. started reading , looks using xdocument better. can see code, have tried getting owner value in 2 ways, both fail.

my xml looks bit this:

    <container xmlns:i="http://www.w3.org/2001/xmlschema-instance" xmlns="dm"> <isconsigned>false</isconsigned> <lockstate>unlocked</lockstate> <sourcetype i:nil="true" /> <id>04216194-4f62-47ee-ab21-c1053d01bf1e</id> <owner>in</owner> <created>2012-08-21t09:29:10.528321+02:00</created> </container> 

and code:

      class program {    static sqlconnection conn = new sqlconnection();    static xdocument xml = new xdocument();      static void main(string[] args)     {         using (conn)         {             conn.connectionstring = properties.settings.default.connectionstring;             //connection.open();             conn.open();             sqldatareader reader = getdatafromdatabase();              if (reader.hasrows)             {                  while (reader.read())                 {                     string xmlfile = reader.getsqlstring(0).tostring();                     handledata(xmlfile);                 }             }             else             {                 console.writeline("no rows found.");             }             reader.close();         }     }      public static void handledata(string xmlin)     {          xml = xdocument.parse(xmlin);         xelement xmlelement = xml.root;         string test1 = xml.element("owner").value.tostring();         string test = xmlelement.element("owner").value.tostring();     }      } 

that wasn't problem of using xmldocument or xdocument. xml has default namespace, namespace declared without prefix, here :

xmlns="dm" 

contrasts 1 prefix i here: xmlns:i="http://www.w3.org/2001/xmlschema-instance". note not element default namespace declared in namespace, descendant elements inherit ancestor default namespace implicitly, unless otherwise specified (using explicit namespace prefix or local default namespace point different namespace uri).

you can use combination of "xnamespace"+"element's local name" form qualified-name referencing element in namespace, example :

var xmlin = @"<container xmlns:i='http://www.w3.org/2001/xmlschema-instance' xmlns='dm'> <isconsigned>false</isconsigned> <lockstate>unlocked</lockstate> <sourcetype i:nil='true' /> <id>04216194-4f62-47ee-ab21-c1053d01bf1e</id> <owner>in</owner> <created>2012-08-21t09:29:10.528321+02:00</created> </container>"; var xml = xdocument.parse(xmlin); xnamespace d = "dm"; string owner = xml.root.element(d+"owner").value; console.writeline(owner); string id = xml.root.element(d+"id").value; console.writeline(id); 

dotnetfiddle demo

output :

in 04216194-4f62-47ee-ab21-c1053d01bf1e 

side notes:

  • element() doesn't recognize xpath expression, accepts xname parameter instead.
  • xelement.value property string already, no need call tostring() on it.
  • similar case reference, if need use xpath xdocument later : use xpath xml namespace

Comments