简体   繁体   中英

Displaying XML data in textbox through C# in Visual Studio 2015

I am currently working on an application that lets the user pick an order for a restaurant. When the user clicks the order and they click submit, it should add to the XML file called "order.xml" which is what happens. However at the same click whatever has been inputted in the file should then be displayed back into a textbox on the same view. I am getting an error marked "Object reference not set to an instance of an object." on the code shown below...

var order = from MenuInfo in doc.Descendants("MenuInfo")
                       select new
                       {
                           Data1 = MenuInfo.Element("Data1").Value,
                           Data2 = MenuInfo.Element("Data2").Value,
                           Data3 = MenuInfo.Element("Data3").Value,
                       };

Could you help me debug this as I am at a stomp.

Full code sample

 XDocument doc = XDocument.Load("order.xml");
        XElement root = new XElement("MenuInfo");
        root.Add(new XElement("Data1", mealListView.SelectedValue.ToString()));
        root.Add(new XElement("Data2", _seat));
        root.Add(new XElement("Data3", buttonTable1.Text));
        doc.Element("MenuInfo").Add(root);
        doc.Save("order.xml");

        var order = from MenuInfo in doc.Descendants("MenuInfo")
                       select new
                       {
                           Data1 = MenuInfo.Element("Data1").Value,
                           Data2 = MenuInfo.Element("Data2").Value,
                           Data3 = MenuInfo.Element("Data3").Value,
                       };

        summaryTextBox.Text = "";
        foreach (var MenuInfo in order)
        {
            summaryTextBox.Text = summaryTextBox.Text + "Meal: " + MenuInfo.Data1 + "\n";
            summaryTextBox.Text = summaryTextBox.Text + " Seat ID: " + MenuInfo.Data2 + "\n";
            summaryTextBox.Text = summaryTextBox.Text + " Table ID: " + MenuInfo.Data3 + "\n";
        }

        if (summaryTextBox.Text == "")
            summaryTextBox.Text = "No Results.";

I'm not 100% certain what the problem is, but I think it might be related to the following.

You create a new element called MenuInfo as the root variable:

XElement root = new XElement("MenuInfo");

Later you then append this new element to another element in the XML also called MenuInfo :

doc.Element("MenuInfo").Add(root);

So now you probably have an element called MenuInfo with a child element called MenuInfo , like this:

<MenuInfo>
    <MenuInfo>
        <Data1>one</Data1>
        <Data2>two</Data2>
        <Data3>three</Data3>
    </MenuInfo>
</MenuInfo>

In your subsequent selection, you will select the parent MenuInfo element which does not have any Data1 , Data2 or Data3 child elements, so when you then do

select new
 {
     Data1 = MenuInfo.Element("Data1").Value,
     Data2 = MenuInfo.Element("Data2").Value,
     Data3 = MenuInfo.Element("Data3").Value,
 };

you're trying to access the Value property on an element that doesn't exist.

You may be better off trying to locate the MenuInfo element in your XML immediately after loading and if it doesn't exist then create it. Otherwise just append the DataN elements to the element that already exists.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM