Smart    Lucky

Smart Lucky

  • NA
  • 555
  • 638.6k

XML Excetption

Aug 15 2011 5:52 AM
Hi

I am using xml in this way and also reading in right but when i add CDATA and reading it giving me exception but why i don't know can any one help me...?

how can i read at a time cdata and attributes ...?



Xml file is

<Employees>
  <Employee id="101A" firstname="naga" lastname="raju">
    <Address>
      <street>prakash nagar</street>
      <area>begumpet</area>
      <city>hyderabad</city>
      <state>andhra pradesh</state>
    </Address>
  </Employee>
  <Employee id="102A" firstname="ratna" lastname="raju">
    <Address>
      <street>srnagar</street>
      <area>ameerpet</area>
      <city>hyderabad</city>
      <state>andhra pradesh</state>
    </Address>
  </Employee>
  <Employee id="103A" firstname="murali" lastname="mohan">
    <!--This is comments content-->
    <Address>
      <street>anna salai</street>
      <area>t nagar</area>
      <city>chennai</city>
      <state>tamilnadu</state>
    </Address>
    <![CDATA[This is cdata content]]>
  </Employee>
</Employees>



Cs File is
  protected void Button1_Click(object sender, EventArgs e)
    {
        XmlDocument doc = new XmlDocument();
        doc.Load(Server.MapPath("~/XMLFile.xml"));
        //XmlNodeList node = doc.GetElementsByTagName("Books");
        XmlNode node = doc.DocumentElement;
        foreach (XmlNode cnode in node)
        {
            if (cnode.HasChildNodes)
            {
                foreach (XmlNode ChildNode in cnode.ChildNodes)
                {
                    Response.Write("<br/>Child node is" + cnode.InnerText + "<br/>"+cnode.NodeType);
                    foreach (XmlAttribute nodeAttribute in ChildNode.Attributes) //Excetptio occured here...// Object reference not set to an instance of an object.
                    {


                        Response.Write("<br/>Attributes is" + nodeAttribute.InnerXml + "<br/>");
                    }
                    ////Response.Write("<br/>Child node is"+cnode.InnerText+"<br/>");
                }
                if (cnode is XmlCDataSection)
                {
                    XmlCDataSection cdatasection = cnode as XmlCDataSection;
                    Response.Write("<br/>CDATA" + cdatasection.InnerText + "<br/>");
                }
               
            }
           
            //Response.Write(cnode.InnerText);
        }
    }

Answers (1)