locked
NullReferenceException RRS feed

  • Question

  • using (XmlReader reader = XmlReader.Create(new StringReader(xmlString)))

    {

        XmlWriterSettings ws = new XmlWriterSettings();

        ws.Indent = true;

        using (XmlWriter writer = XmlWriter.Create(output, ws))

        {

            while (reader.Read())

            {

                switch (reader.NodeType)

                {

                    case XmlNodeType.Element:

                        writer.WriteStartElement(reader.Name);

                        break;

                    case XmlNodeType.Text:

                        writer.WriteString(reader.Value);

                        break;

                    case XmlNodeType.XmlDeclaration:

                    case XmlNodeType.ProcessingInstruction:

                        writer.WriteProcessingInstruction(reader.Name, reader.Value);

                        break;

                    case XmlNodeType.Comment:

                        writer.WriteComment(reader.Value);

                        break;

                    case XmlNodeType.EndElement:

                        writer.WriteFullEndElement();

                        break;

                }

            }

        }

    }

    TextBlock.Text = output.ToString();

    I am using this to get xml data ,but I'm getting a NullReferenceException. Is there a way to test if the elements exist before I try to use the value?

    Thursday, July 17, 2014 7:31 AM

Answers

All replies