1

While upgrading from Tridion 2011 to Web 8.5, unable to read nodes from XML. There are no values going in foreach.

XmlDocument config = new XmlDocument();                
config.LoadXml(xml);          
XmlNodeList doc = config.SelectNodes(Xpath);
foreach (XmlNode x in doc)
{
    HTValue.Add(x.Attributes["name"].Value.ToString(), x.Attributes["value"].Value);
    Console.WriteLine("Hash Name :" + x.Attributes["name"].Value.ToString() + " Hash Value : " + x.Attributes["value"].Value);
}
  • Welcome to Trdionstackexchange swapnil!, Please explain a bit more detail about what you are trying to do? what you are trying access? using core service or TOM.NET API? – Velmurugan Nov 29 at 21:46
  • And how does the variable get filled? With what XML? – Quirijn Nov 30 at 9:32
  • 1
    It clearly looks like the issue is with how the value of xml and xpath are being populated, whether the right namespaces are being used, etc. – Nuno Linhares Nov 30 at 15:42
  • @Velmurugan I am using core services. I checked the count of nodes is coming but when trying to retrieve value it is null. – swapnil shukla Dec 2 at 2:52
  • @Quirijn yes the variable is filled with XML. – swapnil shukla Dec 2 at 2:52
0

Looking at your code and comments about what you are trying to do.

Indeed your code seems to be okay, your XPath value is the one problem based on your input XML and XPath value.

You could try this sample code it works.

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

namespace TestConsoleApp
{
    public class Program
    {
        private static void Main(string[] args)
        {

            string XML = $"<?xml version=\"1.0\" encoding=\"utf-8\"?><configuration><appSettings><!--keys specific to auto-publish--><add key=\"abc\" value=\"xyz\" /><add key=\"abcd\" value=\"pqr\" /><add key=\"abce\" value=\"/Building%20Blocks/Content/def.xml\" /></appSettings></configuration>";
            XDocument document = XDocument.Parse(XML);
            foreach (XElement element in document.Root.XPathSelectElements("/configuration/appSettings/add"))
            {
                string key = element.Attribute("key") != null ? element.Attribute("key").Value : string.Empty;
                string value = element.Attribute("value") != null ? element.Attribute("value").Value : string.Empty;
                Console.WriteLine($"Key: {key}, Value: {value}");
            }
            Console.ReadKey();
        }
    }
}

I hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.