更新xml属性

更新xml属性,xml,vb.net,xpath,Xml,Vb.net,Xpath,我有一个与此类似的XML <list> <sublist id="a"> <item name="name1"> <property1>a</property1> <property2>b</property2> </item> <item name="name2">

我有一个与此类似的XML

<list>
    <sublist id="a">
        <item name="name1">
            <property1>a</property1>
            <property2>b</property2>
        </item>
        <item name="name2">
            <property1>c</property1>
            <property2>d</property2>
        </item>
    </sublist>
    <sublist id="b">
        [...more XML here...]
    </sublist>
</list>
sublistID、itemName和newName作为参数传递给函数(如我前面所说)。我想我在SelectSingleNode中失败了,但我不知道为什么


关于

如果不引发异常,您发布的代码将无法运行。我怀疑您没有看到异常的原因是因为您在Try/Catch块中有这段代码,该块正在处理异常,并且没有通知您发生了异常

错误的原因是您正在选择
元素,但试图将其强制转换为
xmldattribute
变量。从
XmlElement
到和
XmlAttribute
的强制转换失败并引发异常。要修复它,您需要选择
name
属性,而不是
item
元素,如下所示:

<list>
    <sublist id="a">
        <item name="****NEW NAME****">
            <property1>a</property1>
            <property2>b</property2>
        </item>
        <item name="name2">
            <property1>c</property1>
            <property2>d</property2>
        </item>
    </sublist>
    <sublist id="b">
        [...more XML here...]
    </sublist>
</list>
Dim attr As XmlAttribute = DirectCast(xmlData.SelectSingleNode("//sublist[@id='a']/item[@name='name1']/@name"), XmlAttribute)

关键区别在于XPath末尾的
/@name
。注意,我还添加了
DirectCast
。当您对
选项要求严格时,有必要使用
DirectCast
,这几乎是您应该做的。

这将对您有所帮助。我被审判是为了我的目的。。。您可以根据您的要求更改此项

if (System.IO.File.Exists(ConfigurationManager.AppSettings["DBCache"].ToString() + "Frame.xml"))
{
    System.Xml.XmlDocument doc1f = new System.Xml.XmlDocument();
    doc1f.Load(ConfigurationManager.AppSettings["DBCache"].ToString() + "Frame.xml");
    System.Xml.XmlNode rootcomment1f = doc1f.SelectSingleNode("/FrameDetail");
    System.Xml.XmlNodeList nodes1f = rootcomment1f.SelectNodes("Frame");

    if (nodes1f.Count != 0)
    {
        System.Xml.XmlDocument docff = new System.Xml.XmlDocument();
        docff.Load(ConfigurationManager.AppSettings["DBCache"].ToString() + "Frame.xml");
        System.Xml.XmlNode rootcommentff = docff.SelectSingleNode("/FrameDetail");
        System.Xml.XmlNodeList nodesff = rootcommentff.SelectNodes("Frame");
        for (int i = 0; i < nodesff.Count; i++)
        {
            if (nodesff[i].SelectSingleNode("REFERENCEID").InnerText.Trim() == "2")
            {%>
                <center><div style="padding:1px;width:100%;height:100%;"><iframe src="<%=nodesff[i].SelectSingleNode("URL").InnerText%>" frameborder="0" allowtransparency="true" style="background:transparent;" scrolling="auto"></iframe></div></center>           
            <%}
        }
    }
}
if(System.IO.File.Exists(ConfigurationManager.AppSettings[“DBCache”].ToString()+“Frame.xml”))
{
System.Xml.XmlDocument doc1f=new System.Xml.XmlDocument();
Load(ConfigurationManager.AppSettings[“DBCache”].ToString()+“Frame.xml”);
System.Xml.XmlNode rootcomment1f=doc1f.SelectSingleNode(“/FrameDetail”);
System.Xml.XmlNodeList nodes1f=rootcomment1f.SelectNodes(“框架”);
if(nodes1f.Count!=0)
{
System.Xml.XmlDocument docff=new System.Xml.XmlDocument();
Load(ConfigurationManager.AppSettings[“DBCache”].ToString()+“Frame.xml”);
System.Xml.XmlNode rootcommentff=docff.SelectSingleNode(“/FrameDetail”);
System.Xml.XmlNodeList nodesff=rootcommentff.SelectNodes(“框架”);
for(int i=0;i

此示例解决的问题与OP询问的问题不同。OP尝试根据祖先属性的值选择属性。除非我遗漏了什么,否则此示例根本不处理属性。此外,此问题与VB.NET有关。此示例使用C#。u可以将其转换为VB.netSorry以进行delay回答了这个问题。看起来这是正确的。但是现在我得到了一个“System.Xml.XPath.XPathException”,可能是因为SelectSingleNode中XPath的参数,我用您的示例测试了它,它工作得很好,所以一定还有其他原因(可能是因为您真正的Xml文档比示例更复杂?)。如果没有更多的信息或示例来说明这个问题,我真的无能为力,但我建议简化XPath,直到它开始工作,然后一次增加一点复杂性,每一步都对它进行测试,直到你让它准确地选择你想要的。我的XML更复杂,我试图简化它,以便任何人都能找到它很容易理解我的问题。这是一个音乐播放列表,但节点的基本树是相同的。我将尝试更改路径。这是我遇到的错误。我检查了播放列表中是否存在这些元素(test,test1),它们确实存在。-->System.Xml.XPath.XPathException:'//playlist[@ID='test']/Song[@Name'test1']/@“test1”具有无效令牌。您在
@Name='test1'
中缺少等号。此外,您可能希望选择
/@Name
而不是
@test1
if (System.IO.File.Exists(ConfigurationManager.AppSettings["DBCache"].ToString() + "Frame.xml"))
{
    System.Xml.XmlDocument doc1f = new System.Xml.XmlDocument();
    doc1f.Load(ConfigurationManager.AppSettings["DBCache"].ToString() + "Frame.xml");
    System.Xml.XmlNode rootcomment1f = doc1f.SelectSingleNode("/FrameDetail");
    System.Xml.XmlNodeList nodes1f = rootcomment1f.SelectNodes("Frame");

    if (nodes1f.Count != 0)
    {
        System.Xml.XmlDocument docff = new System.Xml.XmlDocument();
        docff.Load(ConfigurationManager.AppSettings["DBCache"].ToString() + "Frame.xml");
        System.Xml.XmlNode rootcommentff = docff.SelectSingleNode("/FrameDetail");
        System.Xml.XmlNodeList nodesff = rootcommentff.SelectNodes("Frame");
        for (int i = 0; i < nodesff.Count; i++)
        {
            if (nodesff[i].SelectSingleNode("REFERENCEID").InnerText.Trim() == "2")
            {%>
                <center><div style="padding:1px;width:100%;height:100%;"><iframe src="<%=nodesff[i].SelectSingleNode("URL").InnerText%>" frameborder="0" allowtransparency="true" style="background:transparent;" scrolling="auto"></iframe></div></center>           
            <%}
        }
    }
}