简体   繁体   English

C# 中的文本框和 Xml

[英]Text boxes and Xml in C#

I just started using VS2010 and C#.我刚开始使用 VS2010 和 C#。

I'm trying to create an app which takes values from two textboxes and adds it to an existing xml file.我正在尝试创建一个应用程序,它从两个文本框中获取值并将其添加到现有的 xml 文件中。

eg.例如。

Text Box 1       Text Box 2
----------       ----------
A                B
C                D
E                F

I want the resultant xml file to be like this.我希望得到的 xml 文件是这样的。

<root>
 <element>
 <Entry1>A</Entry1>
 <Entry2>B</Entry2>
 </element>
</root>

and so on...等等...

Can this be done using C#??这可以使用 C# 来完成吗?

I'm unable to add the entries alternatively ie Entry1 should contain Text Box 1 line #1 and Entry2 Text Box 2 line #1.我无法交替添加条目,即 Entry1 应包含 Text Box 1 line #1 和 Entry2 Text Box 2 line #1。

Any help would be appreciated.任何帮助,将不胜感激。

Thanks谢谢

You need to split the string retrieved from the text box based on the new line like this:您需要根据new line拆分从文本框中检索到的字符串,如下所示:

string[] lines = theText.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);

Once you have values split for each text box, you can use System.xml.linq.xdocument class and loop through the values that you retrieve above.为每个文本框拆分值后,您可以使用System.xml.linq.xdocument class并循环遍历上面检索到的值。

Something like this:像这样的东西:

XDocument srcTree = new XDocument(new XElement("Root",
    new XElement("entry1", "textbox value1")))

You can retrieve a xml document using a linq query or save it in an xml file using the Save method of XDocument您可以使用 linq 查询检索 xml 文档,或使用XDocumentSave方法将其保存在 xml 文件中

The below code will give you a string of XML data from the textboxes:下面的代码将为您提供来自文本框中的一串XML数据:

private string createXmlTags(TextBox textBox1, TextBox textBox2)
    {
        string strXml = string.Empty;
        string[] text1Val = textBox1.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
        string[] text2Val = textBox2.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
        int count = 1;
        IList<XElement> testt = new List<XElement>();

        for (int i = 0; i < text1Val.Count(); i++)
        {
            testt.Add(new XElement("Entry" + count, text1Val[i]));
            while (!String.IsNullOrEmpty(text2Val[i]))
            {
                count = count + 1;
                testt.Add(new XElement("Entry"+count,text2Val[i]));
                break;
            }
            count = count + 1;
        }
        foreach (var xElement in testt)
        {
            strXml += xElement.ToString();
        }
        return strXml;
    }

You can then insert the code to an existing xml document.然后,您可以将代码插入现有的 xml 文档。 Follow: How can I build XML in C#?关注: 如何在 C# 中构建 XML? and How to change XML Attribute以及如何更改 XML 属性

Read here: XDocument or XmlDocument在这里阅读: XDocument 或 XmlDocument

I will have the decency of not copying the code from there.我不会从那里复制代码。 Every basics you need to know on creating a XML doc is well explained there.创建 XML 文档时您需要了解的所有基础知识都在此处得到了很好的解释。

There are two options, I would personally go with XDocument.有两种选择,我个人会用XDocument go。

I know there's no code in this answer but since you haven't tried anything, not even apparently searching Google (believe me, you'd find it), I'd rather point you in the right direction than "giving you the fish".我知道这个答案中没有代码,但是由于您没有尝试过任何东西,甚至没有明显地搜索 Google(相信我,您会找到它),我宁愿为您指出正确的方向,而不是“给您鱼” .

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

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