Share via


Save XML Nodes to Array

Question

Monday, September 29, 2014 6:57 PM

I'm trying to store the values in the ArchiveTask lines in the XML file below into an array. Ultimately I'm trying to get get all ArchiveTask where TaskName contains "ACG" then write that line to a new XML file. I'm not able to get into the values, so any assistance is greatly appreciated.

<?xml version="1.0" encoding="utf-8"?>
<FileArchive>
  <Tasks>
    <ArchiveTask TaskName="ACG-ACH" ArchiveLocation="E:\Logs\_Archive\ACH" ArchiveDirectory="D:\Q2\Logs\ACH" Filter="*.log|*.log.*|*.zip|*.html|*.htm*" Recursive="false" LiveLogDays="1" ArchiveLogDays="30" />
    <ArchiveTask TaskName="Application" ArchiveLocation="E:\Logs\_Archive\Application" ArchiveDirectory="D:\Q2\Logs\Application" Filter="*.log|*.log.*|*.zip|*.html|*.htm*" Recursive="false" LiveLogDays="1" ArchiveLogDays="30" />
  </Tasks>
</FileArchive>

All replies (3)

Tuesday, September 30, 2014 8:28 AM âś…Answered | 1 vote

Hi,

try this

            XmlDocument xml = new XmlDocument();
            xml.Load(@"C:\x.xml");  // suppose that str string contains "<Names>...</Names>"
            XmlNodeList xnList = xml.SelectNodes("FileArchive/Tasks/ArchiveTask[contains(@TaskName,'ACG')]");

            List<string> arr = new List<string>();
            
            foreach (XmlNode xn in xnList)
            {
                arr.Add(xn.OuterXml);
            }

Monday, September 29, 2014 8:14 PM | 1 vote

string[] arr = XDocument.Load(@"C:\xxx.xml").Descendants("TaskName")
                        .Select(element => element.Value).ToArray();

Monday, September 29, 2014 8:54 PM

Thanks, this almost works. The array is empty on TaskName, but has length with ArchiveTask. However printing the contents of that array are all blank.