2013-06-04 43 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」); –

相關問題