2013-07-03 288 views
-3

嗨我有一個XML的字符串,即時消息正在尋找插入另一個使用C#的特定位置的XML塊。將字符串插入字符串C#

<試驗> <組> < /組> <組> < /組> < /測試> <洛爾> <組> < /組> < /洛爾>

我想我最後< /組> <測試后里面nsert字符串>,以便它看起來像

<測試> <組> < /組> <組> < /組> <我加入這組> </I加入該組> < /測試> <洛爾> <組> < /組> < /洛爾>

如果有人知道如何做到這一點,請讓我知道。

在此先感謝。

編輯:

字符串不是隻是一個單一的元件的,但可能數百個元素和子元素和子元素子和許多不同的屬性。 elementception 這是一個示例STRING方式類似,ADDME看起來:

<Group Id="Customers" ResourceId="Group_Customers" DescriptionResourceId="Customers_Description"> 
    <SubArea Id="nav_accts" DescriptionResourceId="Account_SubArea_Description" Entity="account" GetStartedPanePath="Accounts_Web_User_Visor.html" GetStartedPanePathAdmin="Accounts_Web_Admin_Visor.html" GetStartedPanePathOutlook="Accounts_Outlook_User_Visor.html" GetStartedPanePathAdminOutlook="Accounts_Outlook_Admin_Visor.html" /> 
    <SubArea Id="nav_conts" DescriptionResourceId="Contact_SubArea_Description" Entity="contact" GetStartedPanePath="Contacts_Web_User_Visor.html" GetStartedPanePathAdmin="Contacts_Web_Admin_Visor.html" GetStartedPanePathOutlook="Contacts_Outlook_User_Visor.html" GetStartedPanePathAdminOutlook="Contacts_Outlook_Admin_Visor.html" /> 
</Group> 
<Group Id="SFA" ResourceId="Area_Sales" DescriptionResourceId="Sales_Description"> 
    <SubArea Id="nav_leads" DescriptionResourceId="Lead_SubArea_Description" Entity="lead" GetStartedPanePath="Leads_Web_User_Visor.html" GetStartedPanePathAdmin="Leads_Web_Admin_Visor.html" GetStartedPanePathOutlook="Leads_Outlook_User_Visor.html" GetStartedPanePathAdminOutlook="Leads_Outlook_Admin_Visor.html" /> 
    <SubArea Id="nav_oppts" DescriptionResourceId="Opportunity_SubArea_Description" Entity="opportunity" GetStartedPanePath="Opportunities_Web_User_Visor.html" GetStartedPanePathAdmin="Opportunities_Web_Admin_Visor.html" GetStartedPanePathOutlook="Opportunities_Outlook_User_Visor.html" GetStartedPanePathAdminOutlook="Opportunities_Outlook_Admin_Visor.html" /> 
    <SubArea Id="nav_comps" DescriptionResourceId="Competitor_SubArea_Description" Entity="competitor" /> 
</Group> 
<Group Id="Collateral" ResourceId="Area_Collateral" DescriptionResourceId="Area_Collateral_Description"> 
    <SubArea Id="nav_quotes" DescriptionResourceId="Quote_SubArea_Description" Entity="quote" GetStartedPanePath="Quotes_Web_User_Visor.html" GetStartedPanePathAdmin="Quotes_Web_Admin_Visor.html" GetStartedPanePathOutlook="Quotes_Outlook_User_Visor.html" GetStartedPanePathAdminOutlook="Quotes_Outlook_Admin_Visor.html" /> 
    <SubArea Id="nav_orders" DescriptionResourceId="Orders_SubArea_Description" Entity="salesorder" GetStartedPanePath="Orders_Web_User_Visor.html" GetStartedPanePathAdmin="Orders_Web_Admin_Visor.html" GetStartedPanePathOutlook="Orders_Outlook_User_Visor.html" GetStartedPanePathAdminOutlook="Orders_Outlook_Admin_Visor.html" /> 
    <SubArea Id="nav_invoices" DescriptionResourceId="Invoice_SubArea_Description" Entity="invoice" GetStartedPanePath="Invoices_Web_User_Visor.html" GetStartedPanePathAdmin="Invoices_Web_Admin_Visor.html" GetStartedPanePathOutlook="Invoices_Outlook_User_Visor.html" GetStartedPanePathAdminOutlook="Invoices_Outlook_Admin_Visor.html" /> 
    <SubArea Id="nav_products" DescriptionResourceId="Product_SubArea_Description" Entity="product" GetStartedPanePath="Products_Web_User_Visor.html" GetStartedPanePathAdmin="Products_Web_Admin_Visor.html" GetStartedPanePathOutlook="Products_Outlook_User_Visor.html" GetStartedPanePathAdminOutlook="Products_Outlook_Admin_Visor.html" /> 
    <SubArea Id="nav_saleslit" DescriptionResourceId="SalesLit_SubArea_Description" Entity="salesliterature" /> 
</Group> 
<Group Id="MA" ResourceId="Area_Marketing" DescriptionResourceId="Marketing_Description"> 
    <SubArea Id="nav_lists" DescriptionResourceId="MarketingList_SubArea_Description" Entity="list" GetStartedPanePath="Marketing-Lists_Web_User_Visor.html" GetStartedPanePathAdmin="Marketing-Lists_Web_Admin_Visor.html" GetStartedPanePathOutlook="Marketing-Lists_Outlook_User_Visor.html" GetStartedPanePathAdminOutlook="Marketing-Lists_Outlook_Admin_Visor.html" /> 
    <SubArea Id="nav_minicamps" DescriptionResourceId="Quick_Campaign_Description" Icon="/_imgs/ico_18_minicamps.gif" Entity="bulkoperation" GetStartedPanePath="Quick-Campaigns_Web_User_Visor.html" GetStartedPanePathAdmin="Quick-Campaigns_Web_Admin_Visor.html" GetStartedPanePathOutlook="Quick-Campaigns_Outlook_User_Visor.html" GetStartedPanePathAdminOutlook="Quick-Campaigns_Outlook_Admin_Visor.html" OutlookShortcutIcon="/_imgs/olk_4400.ico"> 
    <Privilege Privilege="AllowQuickCampaign" /> 
    </SubArea> 
</Group> 
+0

你嘗試過什麼具體的問題是你有?關於XML解析/修改以及字符串操作,有很多答案。 – jltrem

+1

int index = xml.IndexOf(「」); string go = xml.Insert(index +「」.Length,addme); –

+4

不要使用字符串操作來修改XML。將它加載到一個'XElement'中。 –

回答

2

你可以做這樣的事情,首先加載字符串作爲XML

string xmlContent = "Your xml string content"; 
    //parse the string as xml 
    XDocument xmlDoc = XDocument.Parse(xmlContent); 
    //get the element which matches your need. 
    XElement areaElement = 
     (from el in xmlDoc.Descendants("area") where el.Attribute("id").Value=="workplace" 
     select el).FirstOrDefault(); 
    //create a new group element 
    XElement newGroup = new XElement("i added this group"); 
    //add a value for this element or add attributes what ever is needed to this group here 
    //add the new group this will by default add it at the end of all other child elements. 
    areaElement.Add(newGroup); 
+1

+1用於將XML字符串視爲** XML **而不是字符串。格式和API存在的原因。 :) –

+0

對不起,我應該更清楚。字符串看起來更像這樣:它不是1個元素,它的許多元素和許多子元素。 但類似的方式更長哈哈 –

+0

您的組元素可以有任意數量的屬性或子元素,但如果您只需要創建另一個元素在區域元素的末尾(即添加一個新的組作爲區域元素內的其他組元素的同胞)。如果你不能得到它,那麼請張貼確切的xml和你想添加的元素,這樣我們可以發佈完整的例子,但至少我期望,你應該嘗試:(因爲大部分骨架已經 – srsyogesh