想要在XML文件中的特定位置插入XML元素

时间:2015-10-26 10:53:15

标签: c# xml

我有一个像这样的XML文件

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

我想在上面的XML中插入我希望的位置。

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

假设我想在第二个位置插入Vehicles元素,结果将如下:

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

如何将车辆部件插入特定位置帮助我。

2 个答案:

答案 0 :(得分:0)

如果您知道要放置新元素之后的元素而不是使用XDocument.AddAfterSelf方法(MSDN)。或者将XDocument.AddBeforeSelf放在给定元素之前。

答案 1 :(得分:0)

试试这个:

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

要插入多个元素,每个元素都有自己的索引:

(由于您还没有显示索引如何映射到要插入的元素,我假设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);
    }
}

注意:这将在不存在的索引上崩溃。