将字符串插入字符串C#

时间:2013-07-03 16:05:01

标签: c# string insert

您好我有一串XML,我希望使用C#在特定位置插入另一个XML块。

  

<测试>      <组>       < /组>       <组>        < /组>   < /试验>    < Lol>    <组>        < /组>   < /洛尔>

我想在最后一个<之后插入一个字符串/组>里面<测试>使其看起来像

  

<测试>      <组>       < /组>       <组>        < /组>       <我添加了这个组>        < /我添加了这个组>   < /试验>    < Lol>    <组>        < /组>   < /洛尔>

如果有人知道怎么做,请告诉我。

提前致谢。

编辑:

字符串不仅仅是单个元素,而且可能包含数百个元素和子元素以及子子元素和许多不同的属性。 elementception 这是一个类似于如何看待的样本:

<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>

1 个答案:

答案 0 :(得分: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);