通过C#.net动态创建XML元素 [英] Dynamic creation of elements in XML via C#.net

查看:136
本文介绍了通过C#.net动态创建XML元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友们,

我创建了一个动态文本框并将一个值插入XML.我将代码发送给u参考.
我的XML创建代码.

Hi friends,

I created a Dynamic textbox and insert a value into XML.I send my code for u reference.
My code for XML Creation.

string sFilename = xmlpath;
            XmlDocument xmlDoc = new XmlDocument();
            String XmlSpecification = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>";
            XmlSpecification = XmlSpecification + Environment.NewLine + "<Testing></Testing>";
            File.AppendAllText(xmlpath, XmlSpecification);
            xmlDoc.Load(sFilename);
            XmlElement elmXML = xmlDoc.CreateElement("Configuration");
            string strConfiguration = String.Empty;
            for (int index = 0; index < 3; index++)
            {
                string targetTextBox = "Textbox" + index;
                //Try to find the textbox
                int textBoxIndex = gb.Controls.IndexOfKey(targetTextBox);
                if (textBoxIndex != -1)
                {
                    TextBox foundTextBox = (TextBox)gb.Controls[textBoxIndex];
                    Userprefrenece.txt1 = foundTextBox.Text;
                }
                String str = "port" + index.ToString();
                strConfiguration = strConfiguration + Environment.NewLine + @"<str>"  + Userprefrenece.txt1 + "</str>";
            }
            elmXML.InnerXml = strConfiguration;
            xmlDoc.DocumentElement.AppendChild(elmXML);
            xmlDoc.Save(sFilename);
            MessageBox.Show("Saved Successfully");


我成功保存了.如何从XML中检索.但是我没有弄清楚.
我的XML数据


I saved successfully. How can retrieve from XML. But I didn''t get correct.
My XML Data

<?xml version="1.0" encoding="utf-8" ?>
- <Testing>
- <Configuration>
  <str>NARAAYANAN</str>
  <str>DELL</str>
  <str>SUCCESS</str>
  </Configuration>
  </Testing>


检索是我的问题.如何从xml.中检索数据并将其存储在文本框中.请帮助我紧急

问候,
Lakshmi Narayanan.S


retrieve is my problem. How can I retrieve data from xml.and store in the textbox .Please help me Urgent

Regards,
Lakshmi Narayanan.S

推荐答案

除了Mewara给您的选项之外,您还可以尝试xsd.exe.

为您的xml创建一个架构,然后运行 XML架构定义工具 [ ^ ]文件.

然后,您将拥有类型安全的类,该类使您可以操纵xml文件的内容.

最好的奖品
Espen Harlinn
Apart from the options S Mewara gave you, you can try out xsd.exe.

Create a schema for your xml and run the XML Schema Definition Tool[^] on the xsd file.

You will then have typesafe classes the allows you to manipulate the contents of the xml file.

Best reagrds
Espen Harlinn


我建​​议您使用其他方法.如果您需要保留一些数据,无论是哪种类型,都您不必直接使用XML .

您可以使用数据合同.这是向持久性和任意数据类型结构添加持久性的最非侵入性方式.您只需添加属性[DataContract][DataMember].当您由参与合同的数据类的实例形成一些数据图时(该图甚至不必是树:循环引用将自动成功解析),您可以使用System.Runtime.Serialization.DataContractSerializer保存和恢复数据到任何流.

您的XML模式将被动态创建.对于数据合同,合同的一部分仅是带有属性标记的部分.您可以进行其他任何更改,并且不要担心违反合同的风险.在开发软件的新版本时,逐步地扩展数据模式也非常容易:您只需添加新的合同成员/类型.您无需创建任何从旧版本数据到新版本的转换.

参见:
http://msdn.microsoft.com/en-us/library/ms733127.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system. runtime.serialization.datacontractserializer.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system. runtime.serialization.datacontractattribute.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system. runtime.serialization.datamemberattribute.aspx [ ^ ].

-SA
I would suggest you use different approach. If you need to persist some data, not matter what kind, you don''t have to work with XML directly.

You can use Data Contract. This is the most non-intrusive way to add persistence to and arbitrary data type structure. You just add attributes [DataContract] and [DataMember]. When you have some data graph formed by the instances of you data classes participated in the contract (this graph does not even have to be a tree: circular references will be successfully resolved automatically), you can use System.Runtime.Serialization.DataContractSerializer to save and restore data to any stream.

Your XML schema will be created on the fly. With Data Contract, the part of contract is only what is marked with the attributes. You can change anything else and be not afraid of the risk of breaking the contract. It''s also very easy to evolve your data schema incrementally as you develop new versions of your software: you just add new contract members/types. You won''t need to create any kind of conversion from the data of old version to a new version.

See:
http://msdn.microsoft.com/en-us/library/ms733127.aspx[^],
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer.aspx[^],
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractattribute.aspx[^],
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datamemberattribute.aspx[^].

—SA


System.Xml名称空间提供了用于读取,写入和导航XML数据的功能.
以下链接应有帮助:
如何使用Visual C#.NET将XML数据读入数据集 [从xml文件中检索随机数据 [ ^ ]
使用C#从XML文件读取数据 [ ^ ]
The System.Xml namespace provides functionality for reading, writing and navigating XML data.

Following links should help:
How To Read XML Data into a DataSet by Using Visual C# .NET[^]
Retrieve random data from xml file[^]
Reading data from an XML file using C#[^]


这篇关于通过C#.net动态创建XML元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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