0

I'm using this code to save and restore the XML values ​​but I'm in trouble . Rescue usually works the problem and when I try to load the XML . I get this exception that in the image.

enter image description here

line 105 : string text = el.Attribute("Text").Value;

        void SaveData() {
            XDocument xmlDocument = new XDocument(new XElement("Pages"));
            List<XElement> xmlPages = new List<XElement>();
            foreach(KeyValuePair<string, string> doc in documents)
                xmlDocument.Root.Add(
                    new XElement("Page",
                    new XAttribute("nodeName", GetNodeName(doc.Key)),
                    new XAttribute("pageGuid", doc.Key),
                    new XAttribute("Rtf", doc.Value)));
            xmlDocument.Root.Add(
                new XElement("TextEdit",
                new XAttribute("Text", textBox1.Text)));
            xmlDocument.Save(GetPathToFile());
        }

        void LoadData() {
            try {
                XDocument xmlDocument = XDocument.Load(GetPathToFile());

                rootNode.Nodes.Clear();
                documents.Clear();

                foreach(XElement el in xmlDocument.Root.Elements()) {
                    string nodeName = el.Attribute("nodeName").Value;
                    string pageGuid = el.Attribute("pageGuid").Value;
                    string rtf = el.Attribute("Rtf").Value;
                    string text = el.Attribute("Text").Value;
                    rootNode.Nodes.Add(new DataNode(nodeName, pageGuid));
                    documents.Add(pageGuid, rtf);
                    textBox1.Text = text;
                }
            } catch(Exception ex) {
                MessageBox.Show("No data loaded. Check XML file" + ex.ToString());
            }
            treeList1.RefreshDataSource();
        }
2
  • string text = el.Attribute("Text") != null ? el.Attribute("Text").Value : string.Empty; Nullvalue checking... Commented Sep 15, 2016 at 13:28
  • The same exception , no results . Commented Sep 15, 2016 at 13:31

2 Answers 2

2

The exception is clear: There is not such attribute el.Attribute("Text"), so you can't try to get it's value. Check for attribute existence before getting it's value.

Sign up to request clarification or add additional context in comments.

4 Comments

This is the xml <TextEdit Text="e33413" />
Put a breakpoint and inspect variable's values when the exception happens.
According to the debugger it is not finding the Text field in the XML, he is assimilating the variable another value instead of the Text = "value" XML
@KawyllainyVi There are not such "fields" in xml files, there are nodes and attributes. Somehow,, the "Text" attribute isn't there
0

After research could solve the case.

Solution:

void LoadData() {
            try {
                XDocument xmlDocument = XDocument.Load(GetPathToFile());

                rootNode.Nodes.Clear();
                documents.Clear();

                foreach(XElement el in xmlDocument.Root.Elements()) {
                    switch(el.Name.LocalName) {
                        case "Page":
                            string nodeName = el.Attribute("nodeName").Value;
                            string pageGuid = el.Attribute("pageGuid").Value;
                            string rtf = el.Attribute("Rtf").Value;

                            rootNode.Nodes.Add(new DataNode(nodeName, pageGuid));
                            documents.Add(pageGuid, rtf);
                            break;
                        case "Text":
                            textEdit1.Text = el.Attribute("text").Value;
                            break;
                    }
                }
            } catch(Exception ex) {
                MessageBox.Show("No data loaded. Check XML file");
            }
            treeList1.RefreshDataSource();
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.