简体   繁体   中英

Editing XML file in Silverlight does not work, but why?

Im trying to open and edit XML file inside Silverlight element, but I can't edit it.

My XML file (Customers.xml) looks like this:

<?xml version="1.0"?>
<customers>
  <customer>Joe</customer>
  <customer>Barrel</customer>
</customers>

And my C# logic:

[...]

XDocument xdoc = XDocument.Load("Customers.xml");
            xdoc.Root.Add(new XElement("customer", "Stephano")); //here I wish it to add Stephano as a customer.
            using (var file = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (var stream = file.OpenFile("Customers.xml", FileMode.Create))
                {
                    xdoc.Save(stream); //and here I wish it to save it to the file
                }
            }

PopulateCustomersList();

/\\ here is a function that is used to display the content of XML file, it goes:

private void PopulateCustomersList()
        {
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.XmlResolver = new XmlXapResolver();
            XmlReader reader = XmlReader.Create("Customers.xml");
            reader.MoveToContent();

            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element && reader.Name == "customer")
                {
                    //OutputTextBlock.Text = reader.GetAttribute("first");
                    customersList.Items.Add(new ListBoxItem()
                    {
                        Content = reader.ReadInnerXml()
                    });
                }

                if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "customers")
                {
                    break;
                }
            }

            reader.Close();
        }

In my xaml file I have

<ListBox x:Name="customersList" />

so it gets displayed, but the problem is that only Joe and Barrel gets to be displayed and where is Stephano?

I got this code from various tutorials and forums, I don't quite understand it, I know it may be strange way to do that, but I just can't find out how to do that and I'm trying all sort of things. The funniest thing is that I found on many forums a way to save the file, which looks like this: xdoc.Save("Customers.xml"); but my Visual Studio says that the arguments are wrong, because it is a string. How am I supposed to tell him that its a file?

Okay:

.Save() saves the current XDocument, IE it's going to save the XML file you loaded up here

XDocument xdoc = XDocument.Load("Customers.xml");

So it should be something like (this is coded without any knowledge more than you provided)

XDocument xdoc = XDocument.Load("Customers.xml");
        xdoc.Root.Add(new XElement("customer", "Stephano"));
xdoc.Save();
PopulateCustomersList(xdoc);

private void PopulateCustomersList(XDocument xdoc)
     {
         foreach(XElement in element xdoc.Root.Elements("customer"))
         {
            customersList.Items.Add(new ListBoxItem()
             {
                Content = (string)element;
             }
         }
     }

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