如何将UI表单的所有控件的值作为XML文件存储在本地计算机中 [英] How to Store the values of all the controls of a UI form in the local machine as a XML file

查看:68
本文介绍了如何将UI表单的所有控件的值作为XML文件存储在本地计算机中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





以下要求类似于Microft Word / Excel中的崩溃和恢复概念。



用户将数据输入''n''no。选择文本框并在单个表单中选择''n''no下拉值。因此,每当他输入必须在XML文件中写入的值时。因此,即使他的表单在他再次登录到同一链接时随时崩溃,我们需要为他提供加载本地内存数据的规定。因此,每当他点击按钮加载本地数据时,所有控件都应该加载已经输入的本地数据。





有没有办法实现这个目标?

即使除了保存XML文件之外我们还可以采用其他方法请分享一下....





谢谢和问候,

Mathi。

解决方案

你可以创建 FormClosing中的XML文件具有表示表单上每个控件的节点的事件。

将每个控件表示为XML文件中的单独节点。

例如

对于组合框,你需要创建一个组合框节点,其中所有的decedent节点代表组合框的项目。

 <   combobox     id   = 城市 >  
< item id = 一个 已选择 = True > 孟买< / item >
< item id = two 已选择 = 错误 > 古吉拉特< / item >
< / combobox >



在表单加载事件期间读取XML并为相应的控件分配相应注释的值。

以下是创建XML文件的代码。

  private   static   void  Main()
{
XmlDocument doc = new XmlDocument();
XmlNode docNode = doc.CreateXmlDeclaration( 1.0 UTF-8 null );
doc.AppendChild(docNode);

XmlNode productsNode = doc.CreateElement( products);
doc.AppendChild(productsNode);

XmlNode productNode = doc.CreateElement( product);
XmlAttribute productAttribute = doc.CreateAttribute( id);
productAttribute.Value = 01;
productNode.Attributes.Append(productAttribute);
productsNode.AppendChild(productNode);

XmlNode nameNode = doc.CreateElement( 名称);
nameNode.AppendChild(doc.CreateTextNode( Java));
productNode.AppendChild(nameNode);
XmlNode priceNode = doc.CreateElement( Price);
priceNode.AppendChild(doc.CreateTextNode( Free));
productNode.AppendChild(priceNode);

doc.Save( @ Path);
}



以下是上述代码的结果。

 < span class =code-summarycomment><?  xml     version   =  1.0   编码  =  UTF-8   >  <  产品 >   -  <   product     id   =  01 >  <  名称  >  Java <   / Name  > ;  <  价格 > 免费<   / Price  >  <   / product  >  < span class =code-keyword><   / products  >  



您也可以使用 XDocument 。即使用 System.Xml.Linq;

快速示例

 XDocument doc =  new  XDocument(); 
XElement docElement = new XElement( City );
doc.Add(docElement);
docElement.Add( new XElement( 塔内));
docElement.Add( new XElement( 普纳));
doc.Save( @ Path);


Hi,

The below requirement is similar to the Crash and Recovery concept in Microft Word/Excel.

The user will enter data into ''n'' no. of text boxes and select ''n'' no of drop down values in a single form. So whenever he enters those value that has to be wrriten in an XML file. So that even if his form crashes at anytime when he logs into the same link again we need to provide him a provision that load local memory data. So whenever he clicks the button to load local data all the controls should get loaded with the already entered data which has been stored locally.


Is there a way to achive this ?
Even if there is any other approach that we can follow other than saving in XML file Please share that too....


Thanks & Regards,
Mathi.

解决方案

you can create XML file in FormClosing Event having node representing each control on the Form.
Represent each control as separate node inside the XML file.
eg
For combo-box you need to create combo-box node having all decedent node representing items of combo-box.

<combobox id="City">
<item id="one" selected="True">Mumbai</item>
<item id="two" selected="False">Gujarat</item>
</combobox>


During Form Load event read the XML and assign value of respective note to respective control.
Following is the code to Create XML file.

private static void Main()
       {
           XmlDocument doc = new XmlDocument();
           XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
           doc.AppendChild(docNode);

           XmlNode productsNode = doc.CreateElement("products");
           doc.AppendChild(productsNode);

           XmlNode productNode = doc.CreateElement("product");
           XmlAttribute productAttribute = doc.CreateAttribute("id");
           productAttribute.Value = "01";
           productNode.Attributes.Append(productAttribute);
           productsNode.AppendChild(productNode);

           XmlNode nameNode = doc.CreateElement("Name");
           nameNode.AppendChild(doc.CreateTextNode("Java"));
           productNode.AppendChild(nameNode);
           XmlNode priceNode = doc.CreateElement("Price");
           priceNode.AppendChild(doc.CreateTextNode("Free"));
           productNode.AppendChild(priceNode);

           doc.Save(@"Path");
       }


Following is the result of above mention code.

<?xml version="1.0" encoding="UTF-8"?><products>-<product id="01"><Name>Java</Name><Price>Free</Price></product></products>


You can use XDocument also. ie using System.Xml.Linq;
Quick example

XDocument doc = new XDocument();
XElement docElement=new XElement("City");
doc.Add(docElement);
docElement.Add(new XElement("Thane"));
docElement.Add(new XElement("Pune"));
doc.Save(@"Path");


这篇关于如何将UI表单的所有控件的值作为XML文件存储在本地计算机中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆