简体   繁体   中英

Selecting a specific node from a large XML file

Am looking for a way and best approach to select a particular node from a large XML file using PowerShell or .NET (C# or VB.NET).

Below shows a structure of my XML file:

<?xml version="1.0" encoding="utf-8"?>
<T Id="XXXX" Date="20170102">
  <Node id="POS0030" nodeStatus="Online">
  <Node id="POS0031" nodeStatus="Online">
  <Node id="POS0032" nodeStatus="Online">
  <Node id="POS0033" nodeStatus="Online">
  <Node id="POS0034" nodeStatus="Online">
  <Node id="POS0035" nodeStatus="Online">
  <Node id="POS0036" nodeStatus="Online">
  <Node id="POS0049" nodeStatus="Online">
  <Node id="POS0050" nodeStatus="Online">
  <Node id="POS0097" nodeStatus="Online">
  <Node id="POS0098" nodeStatus="Online">
  <Node id="POS0099" nodeStatus="Online">
</T>

Note: under each node there are a lot of sub nodes too

What I want to do is to select specific such as 'POS0049' and save the file to another location.

Thanks,

I don't know if there is a most efficient solution, but this work.

using System;
using System.Linq;
using System.Xml.Linq;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            var xml = XElement.Load("SOURCEPATH");
            var idList = new string[] { "POS0001", "POS0002", "POS0032", "POS0033" };

            foreach (var node in (from n in xml.Elements("Node")
                                  where !idList.Contains(n.Attribute("id").Value)
                                  select n).ToArray())
                node.Remove();

            xml.Save("DESTINATIONPATH");
        }
    }
}

In C# you can load the xml in a XmlDocument. Search all the nodes that maches your cirteria and add them to a new XmlDocument.

The method will look something like this

 static XmlDocument GetNodes(XmlDocument xmlDocument, string[] nodeIds)
        {
            var resultNodes = nodeIds.Select(x => xmlDocument.DocumentElement.SelectSingleNode($"Node[@id='{x}']")).ToList();

            XmlDocument resultDoc = new XmlDocument();
            resultDoc.AppendChild(resultDoc.ImportNode(xmlDocument.DocumentElement, false));
            foreach (var nd in resultNodes)
            {
                resultDoc.DocumentElement.AppendChild(resultDoc.ImportNode(nd, true));
            }
            return resultDoc;
        }

Here is a sample https://dotnetfiddle.net/Iy6hew

Your XML example is not correctly formated, you should have ="Online"/> and not ="Online">

If your file is correctly formated, you can do it in powershell:

[xml]$xmlfile=Get-Content "C:\temp\yourxmlfile.xml" -raw

$xmlfile.T.node | where {$_.id -notin ('POS0049', 'POS0050', 'POS0033', 'POS0034', 'POS0035', 'POS0036' )} |  %{$xmlfile.T.RemoveChild($_)}

$xmlfile.Save("c:\temp\yournewxmlfile.xml")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM