Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using SelectSingleNode with XPath returns NULL

I trying to modify an XML file with SelectSingleNode. The structure of file is

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ProjectExtensions>
    <Borland.Personality>Delphi.Personality</Borland.Personality>
    <Borland.ProjectType>VCLApplication</Borland.ProjectType>
    <BorlandProject>
      <BorlandProject>
        <Delphi.Personality>
          <Parameters>
            ...
          </Parameters>
          <VersionInfo>
            <VersionInfo Name="IncludeVerInfo">True</VersionInfo>
            <VersionInfo Name="AutoIncBuild">False</VersionInfo>
            <VersionInfo Name="MajorVer">4</VersionInfo>
            <VersionInfo Name="MinorVer">1</VersionInfo>
            <VersionInfo Name="Release">3</VersionInfo>
            <VersionInfo Name="Build">559</VersionInfo>
            <VersionInfo Name="Debug">False</VersionInfo>
            <VersionInfo Name="PreRelease">False</VersionInfo>
            <VersionInfo Name="Special">False</VersionInfo>
            <VersionInfo Name="Private">False</VersionInfo>
            <VersionInfo Name="DLL">False</VersionInfo>
            <VersionInfo Name="Locale">1049</VersionInfo>
            <VersionInfo Name="CodePage">1251</VersionInfo>
          </VersionInfo>
...
...
...

My code on VS C# is

using System.Xml;

namespace xmledit
{
    class Program
    {
        private static void Main(string[] args)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("arm.xml");
            var node = doc.SelectSingleNode("//VersionInfo[@Name='Build']");
            if (node != null)
                node.InnerText = "123";                
            doc.Save("temp.xml");
        }
    }
}

So, i trying to modify Tag VersionInfo with Name="Build", but SelectSingleNode returns NULL. What I doing wrong?

like image 357
Sergey Konkin Avatar asked Jul 25 '26 18:07

Sergey Konkin


2 Answers

Your xml document has an default namespace xmlns="http://schemas.microsoft.com/developer/msbuild/2003" therefore (I assume) you need to use a XmlNamespaceManager.

  • http://msdn.microsoft.com/en-us/library/h0hw012b.aspx
  • http://msdn.microsoft.com/en-us/library/d6730bwt.aspx
like image 105
hr_117 Avatar answered Jul 28 '26 07:07

hr_117


This function will add a namespacemanager to your document. Replace "mysite" with whatever you want. After this, you can select nodes with "mysite:[nodename]".

public static XmlNamespaceManager AttachNamespaces(ref XmlDocument xmldoc)
    {
        XmlNamespaceManager NS = default(XmlNamespaceManager);
        XmlNode rootnode = default(XmlNode);
        string strTest = null;
        string attrname = null;
        string ns = null;

        NS = new XmlNamespaceManager(xmldoc.NameTable);
        rootnode = xmldoc.DocumentElement;
        strTest = GetAttribute(ref rootnode, "xmlns");
        if (string.IsNullOrEmpty(strTest))
        {
            NS.AddNamespace("mysite", "http://www.mysite.com/");
        }
        else
        {
            NS.AddNamespace("mysite", strTest);
        }

        // Add namespaces from XML root tag
        foreach (XmlAttribute attr in rootnode.Attributes)
        {
            attrname = attr.Name;
            if (attrname.IndexOf("xmlns:") == 0 && !string.IsNullOrEmpty(attrname))
            {
                ns = attrname.Substring(7);
                NS.AddNamespace(ns, attr.Value);
            }
        }

        return NS;

}

Helper function:

public static string GetAttribute(ref XmlNode mynode, string AttributeName, string DefaultValue = "")
    {
        XmlAttribute myattr = default(XmlAttribute);
        string rtn = "";

        if (mynode != null)
        {
            myattr = mynode.Attributes[AttributeName];
            if (myattr != null)
            {
                rtn = mynode.Attributes[AttributeName].Value;
            }
        }

        if (string.IsNullOrEmpty(rtn))
            rtn = DefaultValue;

        return rtn;
    }

For instance:

XmlDocument xmldoc = new XmlDocument;
// Load something into xmldoc
XmlNamespaceManager NS = AttachNamespaces(ref XmlDocument xmldoc);
XMLNode mynode = xmldoc.SelectSingleNode("//mysite:VersionInfo[@Name='Build']", NS);
like image 24
Pål Thingbø Avatar answered Jul 28 '26 08:07

Pål Thingbø