-2

I have a XML file like this

<RoadPart>
    <Vehicles>
                <ConfigurationSummaryListPosition>
                  <ConfigurationSummary>Test name</ConfigurationSummary>
                </ConfigurationSummaryListPosition>
    </Vehicles>
    <Vehicles>
                <ConfigurationSummaryListPosition>
                  <ConfigurationSummary>Test name</ConfigurationSummary>
                </ConfigurationSummaryListPosition>
    </Vehicles>
    <Vehicles>
                <ConfigurationSummaryListPosition>
                  <ConfigurationSummary>Test name</ConfigurationSummary>
                </ConfigurationSummaryListPosition>
    </Vehicles>
</RoadPart>

I want to insert below part at a position i wish in above XML.

<Vehicles>
            <ConfigurationSummaryListPositionTest>
              <ConfigurationSummary>Test name</ConfigurationSummary>
            </ConfigurationSummaryListPositionTest>
</Vehicles>

Suppose i want to insert Vehicles element at 2nd position then result will be as follows:

<RoadPart>
    <Vehicles>
                <ConfigurationSummaryListPosition>
                  <ConfigurationSummary>Test name</ConfigurationSummary>
                </ConfigurationSummaryListPosition>
    </Vehicles>
    <Vehicles> // My inserted data
                <ConfigurationSummaryListPositionTest> // My inserted data
                  <ConfigurationSummary>Test name</ConfigurationSummary> // My inserted data
                </ConfigurationSummaryListPositionTest> // My inserted data
    </Vehicles> // My inserted data
    <Vehicles>
                <ConfigurationSummaryListPosition>
                  <ConfigurationSummary>Test name</ConfigurationSummary>
                </ConfigurationSummaryListPosition>
    </Vehicles>
    <Vehicles>
                <ConfigurationSummaryListPosition>
                  <ConfigurationSummary>Test name</ConfigurationSummary>
                </ConfigurationSummaryListPosition>
    </Vehicles>
</RoadPart>

How to insert vehicle element part at specific position help me.

Tejas Thakor
  • 97
  • 12

2 Answers2

0

If you know the element after what you want to place a new one than use XDocument.AddAfterSelf Method (MSDN). Or XDocument.AddBeforeSelf to place before the given element.

Leonid Malyshev
  • 475
  • 1
  • 6
  • 14
0

Try this:

XElement root = XElement.Parse(@"
    <RoadPart>
        <Vehicles>
            <ConfigurationSummaryListPosition>
                <ConfigurationSummary>Test name</ConfigurationSummary>
            </ConfigurationSummaryListPosition>
        </Vehicles>
        <Vehicles>
            <ConfigurationSummaryListPosition>
                <ConfigurationSummary>Test name</ConfigurationSummary>
            </ConfigurationSummaryListPosition>
        </Vehicles>
        <Vehicles>
            <ConfigurationSummaryListPosition>
                <ConfigurationSummary>Test name</ConfigurationSummary>
            </ConfigurationSummaryListPosition>
        </Vehicles>
    </RoadPart>
");
XElement toAdd = XElement.Parse(@"
    <Vehicles>
        <ConfigurationSummaryListPositionTest>
            <ConfigurationSummary>Test name</ConfigurationSummary>
        </ConfigurationSummaryListPositionTest>
    </Vehicles>
");
root.Elements().Skip(1).First().AddBeforeSelf(toAdd);

//Alternatively
root.Elements().Take(1).Last().AddAfterSelf(toAdd);

To insert multiple elements, each one with its own index:

(As you haven't shown how your indexes map to the elements to insert, I'll assume a Dictionary<int,XElement>.)

var dict = new Dictionary<int, XElement>() {
    {3, XElement.Parse("<Inserted id=\"1\" />")},
    {1, XElement.Parse("<Inserted id=\"2\" />")}
};

//We need parse the elements to insert in descending order, or else the indexes 
//will be affected by previous insertions
foreach (var kvp in dict.OrderByDescending(kvp => kvp.Key)) {
    if (kvp.Key == 0) {
        root.AddFirst(kvp.Value);
    } else {
        root.Elements().Take(kvp.Key).Last().AddAfterSelf(kvp.Value);
    }
}

Note: this will crash on a non-existent index.

Zev Spitz
  • 13,950
  • 6
  • 64
  • 136
  • If i have a list that contains index where to insert vehicle elements in XML file then how i do this dynamically. – Tejas Thakor Oct 26 '15 at 12:00
  • @TejasThakor The list you are using should be part of your question. – Zev Spitz Oct 26 '15 at 14:21
  • @TejasThakor Do you want to insert the same element multiple times, or different elements each time? If different elements, how are the indexes mapped to the appropriate elements? – Zev Spitz Oct 26 '15 at 14:22
  • I want to insert different element every time, every time i have to insert element at some index position in XML.For example if i want to insert element in between then? – Tejas Thakor Oct 27 '15 at 04:33