C# 在字符串C中插入字符串

C# 在字符串C中插入字符串,c#,string,insert,C#,String,Insert,嗨,我有一个XML字符串,我想用C在一个特定的位置插入另一个XML块 我想在内部的最后一个之后插入一个字符串,使其看起来像 如果有人知道怎么做,请告诉我 提前谢谢 编辑: 字符串不仅包含单个元素,还可能包含数百个元素、子元素、子元素和许多不同的属性。元素感受 这是一个类似于addme的示例字符串: <Group Id="Customers" ResourceId="Group_Customers" DescriptionResourceId="C

嗨,我有一个XML字符串,我想用C在一个特定的位置插入另一个XML块

我想在内部的最后一个之后插入一个字符串,使其看起来像

<我添加了这个组>

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

提前谢谢

编辑:

字符串不仅包含单个元素,还可能包含数百个元素、子元素、子元素和许多不同的属性。元素感受 这是一个类似于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>

可以这样做,首先将字符串作为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);

您尝试过什么?您遇到了什么具体问题?关于XML解析/修改以及字符串操作,有很多答案。int index=XML.IndexOf;字符串go=xml.Insertindex+.Length,addme;不要使用字符串操作来修改XML。将它加载到XElement中。我有一个XML块,其中有多个元素作为字符串。是否仍要生成该文件并将其添加到XDocument中?我尝试了此操作,但没有成功:sitemapxml.Root.ElementArea.LastNode.AddAfterSelfaddme+1用于将XML字符串视为XML而不是字符串。格式和API的存在是有原因的:是的,对不起,我应该说得更清楚。字符串看起来更像这样:它不是1个元素,而是许多元素和许多子元素。但是,就像hahay一样,您的组元素可以有任意数量的属性或子元素,但如果您只需要在区域元素的末尾创建另一个元素,也就是说,添加一个新组作为区域元素内其他组元素的同级元素,这并不重要。尽管如此,如果您无法获得它,请发布确切的xml和您想要添加的元素,以便我们可以发布完整的示例,但至少我认为,您应该尝试一下:因为大部分框架已经给出。啊,是的,xml是动态的。所以我必须动态地生成区域和属性,这才是问题所在。使用javascript解析zip文件,然后从文件中提取xml的特定部分,并将其作为字符串返回,这样就可以添加xdocument after.im了吗。然后我有了一个插件,它将接收这个xml字符串并将其附加到已经存在的xml中。