0

Hi I have xml file (which is actually msbuild file) that uses different namespace

<?xml version="1.0" ?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup Condition="'$(key)'=='1111'">
          <Key1>Value1</Key1>
          <Key3>Value3</Key3>
    </PropertyGroup>
    <PropertyGroup Condition="'$(key)'=='2222'">
          <Key2>Value2</Key2>
          <Key4>Value4</Key4>
    </PropertyGroup>
</Project>

And Iam trying to get value of 'Key'. But the issue is that child node(key) is decided dynamically in c# code. So I can't just append childname to xpath query. But I have a string variable that has name of child node

XmlDocument xml = new XmlDocument();
xml.Load("ref.props");        
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xml.NameTable);
nsmgr.AddNamespace("ms", "http://schemas.microsoft.com/developer/msbuild/2003");

XmlNode platform_node

 = xml.SelectSingleNode("/ms:Project/ms:PropertyGroup[contains(@Condition, '1111')]", nsmgr);

string child_node_name = get_name();
XmlNode child = platform_node.SelectSingleNode(???);

Then, after getting correct property group, how should I write xpath query so I can get correct value??

in His Steps
  • 3,075
  • 6
  • 30
  • 38
  • possible duplicate of [Which namespace is necessary to use SelectSingleNode() method (using default namespace and can't use the method)](http://stackoverflow.com/questions/14698271/which-namespace-is-necessary-to-use-selectsinglenode-method-using-default-nam) – John Saunders Feb 05 '13 at 02:34
  • And please don't ask the same question over and over again. – John Saunders Feb 05 '13 at 02:34
  • It's not duplicate and same question. I forgot to change the title. – in His Steps Feb 05 '13 at 04:05
  • When you're going to ask question that are so similar to each other, you need to make it clear why they're different. – John Saunders Feb 05 '13 at 06:48

1 Answers1

0

If you are looking for child nodes "*" would likely work. Or simply get all child nodes via XmlNode.ChildNodes and grab Value or InnerText.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • But there can be many child nodes. And I am looking for only one child node. the string variable,child_node_name will have that info – in His Steps Feb 05 '13 at 04:06