Exception in reading XML file

2

For weeks I've tried to resolve this error in reading XML. I'm creating an application for Windows Phone 8 in C #. The moment I click on a subject and so display the page with a listBox.

  

An exception of type 'System.ArgumentNullException' occurred in mscorlib.ni.dll but was not handled in user code

The error occurs in this line of code:

 XDocument xdoc = XDocument.Parse(xml);

How can I resolve this problem?

    
asked by anonymous 10.08.2014 / 23:00

3 answers

1

I am posting an example of how to read an xml from disk and deserializes it to a class. This way you do not have to be picking up each item manually. This works on an old project that I have here. Let's go for the explanations of usage.

This is the template for my xml example.

<Pages> 
  <Page>
    <Name>Page 1</Name>
    <Url>http://www.google.com</Url>
    <XPathExpression></XPathExpression>
  </Page>
  <Page>    
    <Name>Page 2</Name>
    <Url></Url>
    <XPathExpression></XPathExpression>
  </Page>  
</Pages>

This is a class I use to load xml, serialize it and deserialize it.

public class UtilXML
    {
        //Private fields        

        //Public fields 
        public XmlDocument XmlDocument { get; set; }

        public UtilXML() { }

        public void LoadXMLByPath(string FilePath)
        {
            try
            {
                XmlDocument = new XmlDocument();
                XmlDocument.Load(FilePath);               
            }
            catch (System.Xml.XmlException e)
            {
                throw new System.Xml.XmlException(e.Message);

            }
        }

        public void SerializeToXml<T>(T obj, string FileName)
        {
            XmlSerializer XmlSerializer = new XmlSerializer(typeof(T));
            FileStream FileStream = new FileStream(FileName, FileMode.Create);
            XmlSerializer.Serialize(FileStream, obj);
            FileStream.Close();
        }

        public T DeserializeFromXml<T>(string StringXML)
        {
            T Result;
            XmlSerializer XmlSerializer = new XmlSerializer(typeof(T));
            using (TextReader TextReader = new StringReader(StringXML))
            {
                Result = (T)XmlSerializer.Deserialize(TextReader);
            }
            return Result;
        }
    }

Essas são as classes que representam meu xml. Repare nos atributos que decoram essa classe. Eles tem os mesmos nomes dos elementos do xml.

[XmlRoot("Pages")]
    public sealed class Pages
    {
        [XmlElement("Page")]
        public List<Page> Items { get; set; }

        public Pages() 
        {
            Items = new List<Page>(); 
        }
    }
    public sealed class Page
    {
        [XmlElement("Name")]
        public string Name { get; set; }
        [XmlElement("Url")]
        public string Url { get; set; }
        [XmlElement("XPathExpression")]
        public string XPathExpression { get; set; }
    }

Usage example:

Upload xml

UtilXML.LoadXMLByPath(@"C:\temp\Pages.xml");

Deserialize to the Pages class.

Pages = this.UtilXML.DeserializeFromXml<Pages>(this.UtilXML.XmlDocument.InnerXml);
    
08.10.2014 / 23:49
1

Expecting to be relevant, if the problem is related to an empty value there is an alternative. Create an extension method.

Create a class with methods like this

public static class MetodosExtensao
{
    public static string ElementValueOrEmpty(this XElement element)
    {
        if (element != null)
            return element.Value;

        return "";
    }
}

In this case it is the reading of a String, I make one for each type of item.

In the class that will read xml add the class as the extension methods

using MetodosExtensao;

and the XML reading will be as below:

XDocument r; //aqui irá receber o xml     
IList<Classe> Lista = (from retorno in r.Descendants("Data").Elements("values")
                                                 select new Classe
                                                 {
                                                     nome = retornoBusca.Element("nome").ElementValueOrEmpty()
                                                 }).ToList();

That's it. This is the procedure I use, I'm a beginner so I'm sorry for some error in the response or in the code.

    
09.04.2015 / 21:45
0

It is almost certain that the xml variable is null for some reason. Consider validating the value of it before using it to instantiate the XDocument object.

    
11.08.2014 / 00:48