我计划创建一个使用xml作为后端的新闻条目,显示应该如下所示:
日期:2010年3月8日
标题-特别新闻
新闻4一些新闻
新闻3-一些新闻
新闻2-一些新闻
新闻1-一些新闻
日期:2010年3月7日
标题-特别新闻
新闻5一些新闻
新闻4一些新闻
新闻3-一些新闻
新闻2-一些新闻
新闻1-一些新闻
今天的新闻项目应该在顶部,然后标题应该排序-下降(时间),稍后将出现前一天的新闻项目。
我无法想出xml的逻辑,这种逻辑应该在本例中使用。此外,我无法理解如何在xml的"if“语句中检查”今天的日期“。我能得到一些代码样本来理解这种逻辑吗??
-
如何在winform上导出来自textBox1、textBox2和textBox3的数据(visual C#),以便它能够自动创建一个具有正确放置这些数据的xml文件?
让我们说:
textBox1 =名称:
textBox2 =年龄:
textBox3 =卷号
如果我们再次导出数据,那么如果可以附加导出的xml (在EOF中添加新的数据),那就太好了。
有什么想法吗.
发布于 2010-03-06 07:15:03
我找到了答案,并使我的第一次试用的成功。我试图使用xml和C#。所以我开始做一件小事:
以下是按钮单击的工作代码。
private void button1_Click(object sender, EventArgs e)
{
string path = "employee.xml";
if (File.Exists(path))
{
// to append a new user having reset the textboxes
XDocument doc = XDocument.Load(path);
XElement xe = new XElement("user",
new XElement("department", textBox1.Text),
new XElement("name", textBox2.Text),
new XElement("empno", textBox3.Text)
);
doc.Root.Add(xe);
doc.Save(path);
}
else
{
new XDocument(
new XElement("users",
new XElement("user",
new XElement("department", textBox1.Text),
new XElement("name", textBox2.Text),
new XElement("empNo", textBox3.Text)
)
)
).Save(path);
}
textBox1.Text="";
textBox2.Text="";
textBox3.Text="";
MessageBox.Show("Data added successfully","Done!!",MessageBoxButtons.OK);
this.button1.Enabled=false;
f2=new Form2();
f2.FormClosed += Form2_FormClosed;
f2.Show();
}遗憾的是,我无法从这里得到任何帮助(Stackoverflow)来编辑xml.所以我决定和这里的每个人分享这个密码。也许这个答案会帮助像我这样的新人。
这段代码可以用更好的技能和实用程序进行修改,如果有人有时间和兴趣帮助新手,那么请站出来。
祝各位新手好运:)
发布于 2010-02-02 10:59:58
在.NET中创建XML文件的方法很多。您可以使用对象序列化、XmlWriter、XDocument、.
下面是一个例子:
new XDocument(
new XElement("user",
new XElement("name", textBox1.Text),
new XElement("age", textBox2.Text),
new XElement("rollNo", textBox3.Text)
)
).Save("user.xml");可以生成如下所示的XML文件:
<?xml version="1.0" encoding="utf-8"?>
<user>
<name>foo</name>
<age>20</age>
<rollNo>123</rollNo>
</user>发布于 2010-02-02 19:52:28
有无数种方法可以做到这一点。在你描述的情况下,我会采取的方法是:
XDocument或XmlWriter生成XML的类中。我不想做的是:在表单中编写一个方法,从表单控件中提取数据并将它们写入XML。
https://stackoverflow.com/questions/2183420
复制相似问题