سؤال

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??

هل كانت مفيدة؟

المحلول

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top