2012-05-01 42 views
0
<?xml version="1.0" encoding="utf-8"?> 
<mappings> 
    <mapping> 
    <aID iskey="true">ABC</aID> 
    <bID iskey="true">DEF</bID> 
    <SubAccount>PS</SubAccount> 
    <Account>PS</Account> 
    </mapping> 
    <mapping> 
    <aID isKey="true">GHI</aID> 
    <bID isKey="true">PFP</bID> 
    <SubAccount>MS</SubAccount> 
    <!-- I want to add a new node here, how can I do this --> 
    </mapping> 
    <mapping> 
    <aID isKey="true">MNO</aID> 
    <bID isKey="true">BBG</bID> 
    <SubAccount>MAX</SubAccount> 
    </mapping> 
</mappings> 

我想添加一個新的節點,如上述XML中所述。我嘗試了很多,但我無法成功。需要幫助添加一個新的XML節點

XmlDocument xDoc = new XmlDocument(); 
xDoc.Load(filename); 

foreach (XmlNode node in xmlDoc.SelectNodes("/mappings/mapping")) 
{ 
    if (boothIDNode.InnerXml == BoothID) 
    { 
     chkBoothIDExists = true; 
     if (clientIDNode.InnerText == ClientID) 
     { 
      chkClientIDExists = true; 
      for (int j = 2; j < nodelist.Count; j++) 
      { 
       columnNode = nodelist[j]; 
       if (columnNode.Name == column.ToString()) 
       { 
        XmlNode elm = xDoc.CreateNode(XmlNodeType.Element,"Account",null); 
        elm.InnerText = value.ToString();                     
        node.AppendChild(elm); // Error comes here 
       } 
      } 
     } 
    } 
} 

xmlDoc.Save(filename); 

問題解決了。由於我愚蠢的錯誤,問題發生了。基本上有兩個XML文檔,我創建了其他XML文檔的新節點,由於錯誤來了。 THANKs all, XmlDocument xDoc = new XmlDocument(); XmlDocument xmlDoc = new XmlDocument();

ERROR:要插入的節點是從不同的文檔上下文

+3

讓我們看看你已經嘗試了什麼,請。 – Ryan

+0

好的,我添加了代碼 –

+0

我已經添加了代碼。 –

回答

1

爲了添加一個節點,考慮下面的例子:

   XDocument a = XDocument.Parse(@"<?xml version=""1.0"" encoding=""utf-8""?> 
<mappings> 
    <mapping> 
    <aID iskey="true">ABC</aID> 
    <bID iskey="true">FPP</bID> 
    <SubAccount>PS</SubAccount> 
    <Account>PS</Account> 
    </mapping> 
    <mapping> 
    <aID isKey="true">GHI</aID> 
    <bID isKey="true">PFP</bID> 
    <SubAccount>MS</SubAccount> 
    <!-- I want to add a new node here, how can I do this --> 
    </mapping> 
    <mapping> 
    <aID isKey="true">MNO</aID> 
    <bID isKey="true">BBG</bID> 
    <SubAccount>MAX</SubAccount> 
    </mapping> 
</mappings>"); 
a.Descendants("mapping").Skip(1).First().Add(new XElement("aaa", new XAttribute("id", 1))); 

--->

 <mappings> 
    <mapping> 
    <aID iskey="true">ABC</aID> 
    <bID iskey="true">FPP</bID> 
    <SubAccount>PS</SubAccount> 
    <Account>PS</Account> 
    </mapping> 
    <mapping> 
    <aID isKey="true">GHI</aID> 
    <bID isKey="true">PFP</bID> 
    <SubAccount>MS</SubAccount> 
    <!-- I want to add a new node here, how can I do this --> 
    <aaa id="1" /> 
    </mapping> 
    <mapping> 
    <aID isKey="true">MNO</aID> 
    <bID isKey="true">BBG</bID> 
    <SubAccount>MAX</SubAccount> 
    </mapping> 
</mappings> 
+0

你使用的是LINQ嗎? –

+0

@waqarfarooqjanjua呀。 –

+0

我不必使用LINQ。但是我的問題解決了,THANKs給你的時間和評論 –

2

使用這樣的東西,而不是ac#人,但這應該有所幫助。我想insertafter是你所需要的:

XmlNode currNode = xDoc.SelectNodes("/mappings/mapping"); 
    XmlNode elm = xDoc.CreateNode(XmlNodeType.Element,"Account",null); 
    currNode.InsertAfter(elm, currNode.LastChild); 
0
XDocument doc = XDocument.Load(filepath); // filepath is string 
doc.Element(firstnodename).SetElementValue(descendantname,newvalue); //names and value are string 
doc.Save(filepath); // This line optional. And filepath like : @"C:\Users\user\desktop\a.xml" or "C:\\Users\\user\\desktop\\a.xml". You should write with ". 

你可以寫更長的.Element 外匯:

doc.Element(firstnode).Element(secondnode).Element(thirdnode).SetElementValue(fourthnode,valueoffourth);