开发者

How can I filter xml sections and save it as a file

开发者 https://www.devze.com 2023-02-05 09:59 出处:网络
I am a beginner in using LINQ. I have an xml data with the format below <Root> <Global> </Global>

I am a beginner in using LINQ. I have an xml data with the format below

<Root>
    <Global>
    </Global>
    <local>
        <element name="A">
            <subelement name="A">
                <element name="A1">
                    <subelement name="A1">
                        <Property>
                        </Property>
                    </subelement>
                </element>
                <element name="B">
                    <Property>
                    </Property>
                </element>
            </subelement>
            <subelement name="B">
                <element name="A">
                    <Property>
                    </Property>
                </element>
                <element name="B">
                    <Property>
                    </Property>
                </element>
            </subelement>
        </element>
    </local>
</Root>

I want to save each section of the Property element into it's own xml file as below while keeping the hierarchy structure intact

<Root>
    <Global>
    </Global>
开发者_如何转开发    <local>
        <element name="A">
            <subelement name="A">
                <element name="A1">
                    <subelement name="A1">
                        <Property>
                        </Property>
                </subelement>
                </element>
            </subelement>
        </element>
    </local>
</Root>

and

<Root>
    <Global>
    </Global>
    <local>
        <element name="A">
            <subelement name="A">
                <element name="B">
                    <Property>
                    </Property>
                </element>
            </subelement>
        </element>
    </local>
</Root>

and so on for all the Property elements. Each element can have a child subelement and each subelement can have element child

Can you provide a solution on how to save each section of Property element as a new xml file with the current hierarchy structure intact. I have tried using the Ancestor() method of the XElement class

XDocument xml = XDocument.Load(filename);
XDocument convertedQuery = new XDocument(
        new XElement(xml.Root.Name, from x in xml.Descendants("local")
                                    select x.Ancestors())
         );

but it is returning other Property elements as well, as below

<Root>
<Global></Global>
<local>
  <element name="A">
    <subelement name="A">
      <element name="A1">
        <subelement name="A1">
          <Property></Property>
        </subelement>
      </element>
      <element name="B">
        <Property></Property>
      </element>
    </subelement>
    <subelement name="B">
      <element name="A">
        <Property></Property>
      </element>
      <element name="B">
        <Property></Property>
      </element>
    </subelement>
  </element>
</local>

Any help would be much appreciated. Thanks


Here is some C# sample that should do what you want or at least give you an idea on how to approach that. The sample for testing simply writes each created XDocument to Console.Out but of course you could change that to save to a file on disk:

static void Main(string[] args)
{
    XDocument doc = XDocument.Load(@"..\..\XMLFile1.xml");
    foreach (XElement prop in doc.Descendants("Property"))
    {
        XDocument result = new XDocument(
            new XElement(doc.Root.Name, doc.Root.Attributes(),
                doc.Root.Elements("Global"),
                CopySubtree(prop.Ancestors("local").First(), prop)));
        result.Save(Console.Out);
        Console.WriteLine();
    }
}
static XElement CopySubtree(XElement ancestor, XElement descendant)
{
    if (ancestor == descendant)
    {
        return descendant;
    }
    else
    {
        return
            new XElement(
                ancestor.Name, 
                ancestor.Attributes(), 
                CopySubtree(
                  ancestor
                  .Elements()
                  .First(e => e.DescendantsAndSelf().Any(d => d == descendant)),
                  descendant));
    }
}

[edit] For the new requirement to copy not only the Property ancestors but also its siblings you could adapt above method as follows:

static XElement CopySubtree(XElement ancestor, XElement descendant)
{
    if (ancestor == descendant.Parent)
    {
        return ancestor;
    }
    else
    {
        return
            new XElement(
                ancestor.Name, 
                ancestor.Attributes(), 
                CopySubtree(
                  ancestor
                  .Elements()
                  .First(e => e.DescendantsAndSelf().Any(d => d == descendant)),
                  descendant));
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号