如何保存为 XML?

发布于 2024-12-16 01:03:47 字数 589 浏览 0 评论 0原文

我目前正在制作一个新的 WP7 应用程序,该应用程序应该使用户能够填写两个文本框,单击按钮,并使内容成为 .XML 文件的一部分。

此时,我使用以下代码:

string ItemName = ItemNameBox.Text;
string ItemAmount= ItemAmountBox.Text;

string xmlString = "<Items><Name>"+ItemName+"</Name></Item>";
XDocument document = XDocument.Parse(xmlString);
document.Root.Add(new XElement("Amount", ItemAmount));
string newxmlString = document.ToString();

MessageBox.Show(newxmlString);

现在,当我单击按钮(因为这是在 onclick 按钮事件内)时,MessageBox 向我显示输出在 XML 方面是正确的。

但是,如何将其放入现有的 XML 模式中,该模式应该能够包含相当多的行(例如,待办事项/购物清单)?

I'm currently making a new WP7 application that should enable users to fill in two textboxes, click a button, and make the content become a part of a .XML file.

At this point, I'm using the following code:

string ItemName = ItemNameBox.Text;
string ItemAmount= ItemAmountBox.Text;

string xmlString = "<Items><Name>"+ItemName+"</Name></Item>";
XDocument document = XDocument.Parse(xmlString);
document.Root.Add(new XElement("Amount", ItemAmount));
string newxmlString = document.ToString();

MessageBox.Show(newxmlString);

Now, when I click the button (as this is within an onclick button event), the MessageBox shows me that the output is correct XML-wise.

However, how do I get this into an existing XML schema, which should be able to contain quite a few rows (to-do/shopping list, for example)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

骷髅 2024-12-23 01:03:47

这是我为 Windows Phone 编写的通用保护程序。

public static void SaveDataToIsolatedStorage(string filePath, FileMode fileMode, XDocument xDoc)
{
    using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
    using (IsolatedStorageFileStream location = new IsolatedStorageFileStream(filePath, fileMode, storage))
    {
        System.IO.StreamWriter file = new System.IO.StreamWriter(location);
        xDoc.Save(file);
    }
}

This is a generic saver I have written for windows phone.

public static void SaveDataToIsolatedStorage(string filePath, FileMode fileMode, XDocument xDoc)
{
    using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
    using (IsolatedStorageFileStream location = new IsolatedStorageFileStream(filePath, fileMode, storage))
    {
        System.IO.StreamWriter file = new System.IO.StreamWriter(location);
        xDoc.Save(file);
    }
}
你与昨日 2024-12-23 01:03:47

如果您想添加多“行”数据,您可以这样做:

// create XML element representing one "Item", containing a "Name" and an "Amount" 
// and add it to the given parent element
private void AddItem(XElement parent, string itemName, int amount)
{
    // create new XML element for item
    XElement newItem = new XElement("Item");
    // add the name
    newItem.Add(XElement.Parse("<Name>" + itemName + "</Name>"));
    // add the amount
    newItem.Add(XElement.Parse("<Amount>" + amount + "</Amount>"));
    // add to parent XML element given by caller
    parent.Add(newItem);
}

private void button1_Click(object sender, RoutedEventArgs e)
{
    // create new document (in your case you would do this only once, 
    // not on every button click)
    XDocument doc = XDocument.Parse("<Items />");

    // doc.Root is <Items /> - lets add some items
    AddItem(doc.Root, "My item", 42);
    AddItem(doc.Root, "Another item", 84);

    // check if we succeeded (of course we did!)
    Debug.WriteLine(doc.ToString());
}

AddItem 可以多次调用,每次调用都会向您的添加一项。元素。每次用户单击按钮时您都会调用它。

XML 的结构如下所示:

<Items>
  <Item>
    <Name>My item</Name>
    <Amount>42</Amount>
  </Item>
  <Item>
    <Name>Another item</Name>
    <Amount>84</Amount>
  </Item>
</Items>

编辑:

以及将 XML 保存到独立存储或从独立存储加载 XML:

using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
{
    using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream("myfile.xml", System.IO.FileMode.Create, isf))
    {
        doc.Save(stream);
    }
}


using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
{
    using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream("myfile.xml", System.IO.FileMode.Open, isf))
    {
        doc = XDocument.Load(stream);
    }
}

If you want to add multiple "rows" of data you could do it like this:

// create XML element representing one "Item", containing a "Name" and an "Amount" 
// and add it to the given parent element
private void AddItem(XElement parent, string itemName, int amount)
{
    // create new XML element for item
    XElement newItem = new XElement("Item");
    // add the name
    newItem.Add(XElement.Parse("<Name>" + itemName + "</Name>"));
    // add the amount
    newItem.Add(XElement.Parse("<Amount>" + amount + "</Amount>"));
    // add to parent XML element given by caller
    parent.Add(newItem);
}

private void button1_Click(object sender, RoutedEventArgs e)
{
    // create new document (in your case you would do this only once, 
    // not on every button click)
    XDocument doc = XDocument.Parse("<Items />");

    // doc.Root is <Items /> - lets add some items
    AddItem(doc.Root, "My item", 42);
    AddItem(doc.Root, "Another item", 84);

    // check if we succeeded (of course we did!)
    Debug.WriteLine(doc.ToString());
}

AddItem can be called multiple times and every call adds one item to your <Items> element. You would call it every time the user clicks the button.

The structure of your XML then looks like this:

<Items>
  <Item>
    <Name>My item</Name>
    <Amount>42</Amount>
  </Item>
  <Item>
    <Name>Another item</Name>
    <Amount>84</Amount>
  </Item>
</Items>

Edit:

And for saving and loading XML to/from isolated storage:

using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
{
    using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream("myfile.xml", System.IO.FileMode.Create, isf))
    {
        doc.Save(stream);
    }
}


using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
{
    using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream("myfile.xml", System.IO.FileMode.Open, isf))
    {
        doc = XDocument.Load(stream);
    }
}
分分钟 2024-12-23 01:03:47

我认为这是编写 XML 文件的更好且正确的方法。你可以在循环中使用这个片段
编写你想要的整个xml

IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream
XmlWriterSettings settings = new XmlWriterSettings();        
settings.Indent = true;   
XmlWriter writer = new XmlWriter(isoStream, settings);



        writer.Formatting = Formatting.Indented;
        writer.Indentation = 3;
        writer.WriteStartDocument();
        writer.WriteStartElement("elementName");

        writer.WriteAttributeString("attName", "value");
        writer.WriteValue("value of elem");
        writer.WriteEndElement();
        writer.WriteEndDocument();
        writer.Close();            

i think this is a better and the right way to write XML file. you can use this snippet in a loop
to write the whole xml you want

IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream
XmlWriterSettings settings = new XmlWriterSettings();        
settings.Indent = true;   
XmlWriter writer = new XmlWriter(isoStream, settings);



        writer.Formatting = Formatting.Indented;
        writer.Indentation = 3;
        writer.WriteStartDocument();
        writer.WriteStartElement("elementName");

        writer.WriteAttributeString("attName", "value");
        writer.WriteValue("value of elem");
        writer.WriteEndElement();
        writer.WriteEndDocument();
        writer.Close();            
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文