2013-06-04 41 views
0

我一直在努力做这在C#编写的XML文件一类我的文件夹中从MyXmlElement值代替NULL值MyXmlElement12如下+ datetimestamp:C#中的另一个标签值替换一个XML标记值

<MyXmlType> 
    <MyXmlElement>Value</MyXmlElement> 
    <MyXmlElement12></MyXmlElement12> 
</MyXmlType> 

有人可以帮忙吗?我已经能够从第一个元素获取值并添加如下的时间戳。但是,如何更新第二个xml标签,其值为replacestring我有下面的内容?

public Form1() 
{ 
    InitializeComponent(); 
    XmlDocument doc = new XmlDocument(); 
    doc.Load("C:\\Users\\1\\1.xml"); 

    XmlNode node = doc.DocumentElement.SelectSingleNode("//MyXmlElement"); 

    string text = node.InnerText; 
    string t = text + DateTime.Now.ToString(); 
    replacestring= t.Replace("/", ""); 
} 

回答

1
XDocument doc = XDocument.Load(Path); 
doc.Element("MyXmlType") 
    .Element("MyXmlElement12") 
    .Value += DateTime.Now.ToString(); 
doc.Save(Path); 
+0

我使用XmlDocument的上面,这是一样的XDocument?有没有一种方法可以使用XMLdocument完成? –

+0

XmlDocument与XDocument不同。 XDocument允许你使用LINQ。我没有亲自使用过XmlDocument,但你可以在这里看到不同之处。 http://stackoverflow.com/questions/1542073/xdocument-or-xmldocument – arunlalam

+0

我得到它的工作非常感谢。我如何循环浏览文件夹中的每个xml文件,我现在正在执行XDocument doc1 = XDocument.Load(“C:\\ Users \\ 1.xml”); –

相关问题