read null values and return null from a xml file C# win froms

shatowolf

New Member
\[code\]<?xml version="1.0" encoding="utf-8"?><configuration version="45.2012.4.23" xmlns="http://www.example.com/"> <description>example.com</description> <reading /> <connection> <sourceId>452342341</sourceId> <organization/> <field>*</field> </connection> <source> <sourceId>452342341</sourceId> <connectionContext> <id /> <name>testing</name> <description /> <contextType>Section</contextType> <organization/> <field>Demo Field</field> <section>testing</section> <subSection /> </connectionContext> <Mode>Section</Mode> <activity>bell Testing</activity> </source></configuration>\[/code\]I want to read this xml and display data in the \[code\]textboxes\[/code\] of windows form. When i select the xml file in a listbox I want to read first set of data from \[code\]connection\[/code\] tag and second set of data from \[code\]connectionContext\[/code\] tag, display the values in the textboxes.The problem when there is a null value the below code is not working??? \[code\] private void DisplayFile(string path) { var doc = XDocument.Load(path); var ns = doc.Root.GetDefaultNamespace(); var conn = doc.Root.Element(ns + "connection"); textBox1.Text = conn.Element(ns + "sourceId").Value; textBox3.Text = conn.Element(ns + "description").Value; textBox4.Text = conn.Element(ns + "uri").Value; textBox5.Text = conn.Element(ns + "username").Value; var conn1 = doc.Root.Element(ns + "connectionContext"); textBox7.Text = conn1.Element(ns + "field").Value; textBox8.Text = conn1.Element(ns + "bellName").Value; textBox9.Text = conn1.Element(ns + "id").Value; textBox10.Text = conn1.Element(ns + "bellboreName").Value; }\[/code\]Error message \[code\]Object reference not set to an instance of an object. Object reference not set to an instance of an object.\[/code\] at this field \[code\](ns + "field").Value;\[/code\]
 
Back
Top