2013-01-17 34 views
-3

我使用一個XML文件,其內容是像下面如何在XML文件中插入值

<Invoices> 
    <Invoice> 
     <Type>[Type]</Type> 
     <Contact></contact> 
    </Invoice> 
<Invoices> 

,並在我的課我想,以填補在對象 我的數據來的XML ontent想在<Type>[Type]</Type>中用obj.type 替換[type],我該如何實現這一點。只是想一個想法。

這是我的代碼:

foreach (XmlNode pnode in xmlParentNode) 
{ 
    pnode.InnerText = objInvoice.Invoice_type; 
    xmlRequestNode = pnode.SelectNodes("Contact"); 
    // var app = xdoc.Root.Descendants("Appliance").SingleOrDefault(e => (string)e.Element("Name") == applianceName); 
    foreach (XmlNode item in xmlRequestNode) 
    { 
     if (item.Name == "ContactNumber") 
     { 
      item.InnerText = objInvoice.ContactNumber.ToString(); 
     } 
    } 
} 

在此先感謝。

+0

你嘗試過什麼?這不是一個複雜的任務,它將替換XMLDocument或XDocument中的某些值或節點。那麼你在哪裏遇到問題? – ryadavilli

+0

感謝您分享您的代碼。您的XML結構中的ContactNumber在哪裏? 'xmlParentNode'是對''節點還是''的引用? XML中是否有多個''或只有一個? – JLRishe

+0

Seens這裏的問題已經改變。而你的代碼現在與你想要做的事沒有任何關係。 –

回答

1
XmlDocument doc = new XmlDocument(); 
doc.Load(path); 

foreach (XmlNode node in doc.GetElementsByTagName("Type[. = '[Type]']")) 
    node.InnerText = "[Obj.Type]"; 

doc.Save(path); 
+0

非常感謝。我怎麼能在許多其他節點插入值 – Amar

1

如何:

XmlDocument doc = new XmlDocument(); 
doc.LoadXml(xmlString); 

foreach (XmlNode typeNode in doc.SelectNodes("/Invoices/Invoice/Type[. = '[Type]']")) 
{ 
    typeNode.InnerText= obj.type; 
} 

string modifiedXml = doc.OuterXml; 
+0

非常感謝你。以及如何在其他節點中插入值 – Amar

+0

您可以按照類似的方法在其他節點中插入值。如果您有具體的例子,請將其添加到問題中。 – JLRishe

+0

我正在做這樣的事情得到問題。 – Amar