简体   繁体   English

在Visual Studio 2015中通​​过C#在文本框中显示XML数据

[英]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. 当用户单击订单并单击提交时,它将添加到名为“ order.xml”的XML文件中。 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. 我不确定100%是什么问题,但我认为可能与以下问题有关。

You create a new element called MenuInfo as the root variable: 您创建一个名为MenuInfo的新元素作为root变量:

XElement root = new XElement("MenuInfo");

Later you then append this new element to another element in the XML also called MenuInfo : 然后,您随后将此新元素附加到XML中的另一个元素,也称为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 ,就像这样:

<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 在随后的选择中,您将选择不包含任何Data1Data2Data3子元素的父MenuInfo元素,因此当您这样做时

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. 您正在尝试访问不存在的元素上的Value属性。

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. 您最好在加载后立即尝试在XML中找到MenuInfo元素,如果不存在,则创建它。 Otherwise just append the DataN elements to the element that already exists. 否则,只需将DataN元素追加到已经存在的元素上即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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