在xml文件中获取路径

时间:2011-04-18 06:01:41

标签: c# xml

我的组合框填充了

6.70_Extensions

7.00_Extensions

7.10.000_Tip

7.10_Extensions

如果我的组合框选择值是6.70_Extensions,并且在按钮事件期间,如何从{x}文件(antcheckin.xml)中选择"6.70_Extensions"的路径?例如:从xml文件中选择"C:\Work\6.70_Extensions\folder"

注意:组合框选择的值是

var buildStream =((DataRowView)BuildstreamComboBox.SelectedItem).Row["value"].ToString()

基本上选择此路径后,我将使此文件夹不是非读取的。

所以这就是我之前所做的(注意:这个组合框最初没有填充xml数据,我设法用xml数据填充它,因此我需要一个新的代码,因为我不能依赖于选择的索引现在)。

// the following is used to ensure the folder checked out is now not "read only"


if (BuildstreamComboBox.SelectedIndex == 0)
        {
            var di = new DirectoryInfo("C:\\Work\\6.70_Extensions\\folder");
            foreach (var file in di.GetFiles("*", SearchOption.AllDirectories))
                file.Attributes &= ~FileAttributes.ReadOnly;
        }
        if (BuildstreamComboBox.SelectedIndex == 1)
        {
            var di = new DirectoryInfo("C:\\Work\\7.00_Extensions\\folder");
            foreach (var file in di.GetFiles("*", SearchOption.AllDirectories))
                file.Attributes &= ~FileAttributes.ReadOnly;
        }
        if (BuildstreamComboBox.SelectedIndex == 2)
        {
            var di = new DirectoryInfo("C:\\Work\\7.10.000_Tip\\folder");
            foreach (var file in di.GetFiles("*", SearchOption.AllDirectories))
                file.Attributes &= ~FileAttributes.ReadOnly;
        }

antcheckin.xml:

<project>
    <buildmachine4>
            <checkout1 exe="wco" folder='-f -R "C:/Work/6.70_Extensions/folder/"'/>
            <checkout2 exe="wco" folder='-f -R "C:/Work/7.00_Extensions/folder/"'/> 
        </buildmachine4>
        <buildmachine5>

        <checkout3 exe="wco" folder='-f -R "C:/Work/7.10.000_Tip/folder/"'/>
    </buildmachine5>

</project>

编辑1:大家好,我将搜索我的antcheckin.xml文件,但我有点不能解析该属性:

public void BuildButton_Click(object sender, RoutedEventArgs e)
{
    // the following is used to ensure the folder checked out is now not "read only"

    // load your XML
    XDocument doc = XDocument.Load(@"C:\Work\ANTCheckIN.xml");


    XElement mainItem = doc.Descendants("project")
                  .Where(mi => mi.Attribute("folder").Value.Contains(buildStream)

    ....
    //what should be added here?
}

1 个答案:

答案 0 :(得分:0)

使用LINQ TO XML并在xml中搜索属性值

如下所示

from el in root.Elements("rootlement")
where
(from add in el.Descendants()
where
add.Attribute("MyAttribute") != null
&&
add.Attribute("MyAttribute").Value.Contains("ZXCV")
select add)
.Any()
select el;