Search code examples
c#stringasp.net-corestringbuilderc#-7.0

How to get the desired substring from a string(xml) in an efficient way?


I have C# application. Below is my string

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

What I need is a substring from the above xml string as below.

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

I tried to get it as below.

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");

The above snippet always fetches the first substring & to me it looks very inefficient.

How can I get the desired substring from a string(xml) in an efficient way?

Thanks!


Solution

  • Just parse the xml and remove the undesired elements:

    XDocument doc = XDocument.Load("fileName.xml");
    //or
    //XDocument doc = XDocument.Parse(textString);
    foreach(var removeNode in new string[]{"add_on_type", "unit_amount_in_cents","revenue_schedule_type"})
    {
        doc.Root.Descendants(removeNode).Remove();
    }
    string result = doc.ToString();
    

    Edit: To add more elements, do this way:

    doc.Root.Add(
        new XElement(
            "subscription_add_on", 
            new XElement("add_on_code","add_on_code_value"),
            new XElement("quantity",
                new XAttribute("type","integer"),
                1
            )
        )
    );