У меня есть C#
заявка.Ниже моя строка
<subscription_add_ons type="array">
<subscription_add_on>
<add_on_type>fixed</add_on_type>
<add_on_code>bike-o-vision</add_on_code>
<unit_amount_in_cents type="integer">2000</unit_amount_in_cents>
<quantity type="integer">1</quantity>
<revenue_schedule_type>evenly</revenue_schedule_type>
</subscription_add_on>
<subscription_add_on>
<add_on_type>fixed</add_on_type>
<add_on_code>boxx</add_on_code>
<unit_amount_in_cents type="integer">1499</unit_amount_in_cents>
<quantity type="integer">1</quantity>
<revenue_schedule_type>evenly</revenue_schedule_type>
</subscription_add_on>
<subscription_add_on>
<add_on_type>fixed</add_on_type>
<add_on_code>fitfusion-strala</add_on_code>
<unit_amount_in_cents type="integer">500</unit_amount_in_cents>
<quantity type="integer">1</quantity>
<revenue_schedule_type>evenly</revenue_schedule_type>
</subscription_add_on>
</subscription_add_ons>
Мне нужна подстрока из приведенной выше строки xml, как показано ниже.
<subscription_add_ons type="array">
<subscription_add_on>
<add_on_code>bike-o-vision</add_on_code>
<quantity type="integer">1</quantity>
</subscription_add_on>
<subscription_add_on>
<add_on_code>boxx</add_on_code>
<quantity type="integer">1</quantity>
</subscription_add_on>
<subscription_add_on>
<add_on_code>fitfusion-strala</add_on_code>
<quantity type="integer">1</quantity>
</subscription_add_on>
</subscription_add_ons>
Я попытался получить ее, как показано ниже.
var xml = _xml.GetXmlNodes(xmlString);
StringBuilder sb = new StringBuilder();
sb.Append("<subscription>");
foreach (XmlNode node in xml)
{
var sIndex = node.OuterXml.IndexOf("<add_on_code>");
var eIndex = node.OuterXml.IndexOf("</add_on_code>");
var subs = "<subscription_add_on>" + node.OuterXml.Substring(sIndex, (eIndex - sIndex)) + "<quantity>1</quantity>" + " </subscription_add_on>";
sb.Append(subs);
}
sb.Append("</subscription");
Приведенный выше фрагмент всегда извлекает первую подстроку, и для меня это выглядит очень неэффективно.
Как эффективно получить нужную подстроку из строки (xml)?
Спасибо!