Xml 基于节点属性值覆盖元素值

Xml 基于节点属性值覆盖元素值,xml,vb.net,visual-studio-2010,linq-to-xml,Xml,Vb.net,Visual Studio 2010,Linq To Xml,我的xml文档的结构如下: <Root> <Word ID="23"> <Type>auxiliary</Type> <English></English> <Thai></Thai> <Meaning></Meaning> <Audio>Dictionary Resources\Sound Files\23.wma<

我的xml文档的结构如下:

<Root>
  <Word ID="23">
    <Type>auxiliary</Type>
    <English></English>
    <Thai></Thai>
    <Meaning></Meaning>
    <Audio>Dictionary Resources\Sound Files\23.wma</Audio>
    <Picture>Dictionary Resources\Picture Files\23.jpg</Picture>
    <Transliteration></Transliteration>
    <Timestamp />
  </Word>
...
</Root>

有多种方法可以做到这一点。您可以将
@
语法与
XDocument
对象一起使用,向下钻取到所需的元素,如下所示:

    Sub Timestamp(ByVal IDChosen As String)
    Dim Dictionary As XDocument = XDocument.Load("Dictionary Resources\Dictionary.xml")
    Dictionary.Root.Elements("Word").Atrribute"ID"(IDChosen).Value = DateTime.Now
    Dictionary.Save("Dictionary Resources\Dictionary.xml")
    End Sub
Sub Timestamp(idChosen As String)
    Dim doc As XDocument = XDocument.Load("Dictionary Resources\Dictionary.xml")
    Dim word As XElement = doc.<Root>.<Word>.FirstOrDefault(Function(x) x.@ID = idChosen)
    If word IsNot Nothing Then
        word.<Timestamp>.First().SetValue(Date.Now)
        doc.Save("Dictionary Resources\Dictionary.xml")
    End If
End Sub
Sub Timestamp(idChosen As String)
    Dim doc As XDocument = XDocument.Load("Dictionary Resources\Dictionary.xml")
    Dim timeStamp As XElement = doc.XPathSelectElement("/Root/Word[@ID='" & idChosen & "']/Timestamp")
    If timeStamp IsNot Nothing Then
        timeStamp.SetValue(Date.Now)
        doc.Save("Dictionary Resources\Dictionary.xml")
    End If
End Sub
Sub Timestamp(idChosen As String)
    Dim doc As XDocument = XDocument.Load("Dictionary Resources\Dictionary.xml")
    Dim t As XElement = _
        (
        From word In doc.<Root>.<Word> 
        Where word.@ID = idChosen 
        Select word.<Timestamp>.FirstOrDefault()
        ).FirstOrDefault()
    If t IsNot Nothing Then
        t.SetValue(Date.Now)
        doc.Save("Dictionary Resources\Dictionary.xml")
    End If
End Sub
Sub Timestamp(idChosen As String)
    Dim doc As XDocument = XDocument.Load("Dictionary Resources\Dictionary.xml")
    Dim t As XElement = _
        (
        From word In doc.Root.Elements("Word") 
        Where word.Attributes("ID").Any(Function(x) x.Value = idChosen) 
        Select word.Element("Timestamp")
        ).FirstOrDefault()
    If t IsNot Nothing Then
        t.SetValue(Date.Now)
        doc.Save("Dictionary Resources\Dictionary.xml")
    End If
End Sub
Dim word As XElement = doc...<Word>.FirstOrDefault(Function(x) x.@ID = idChosen)
或者,可以使用查询语法选择元素,如下所示:

    Sub Timestamp(ByVal IDChosen As String)
    Dim Dictionary As XDocument = XDocument.Load("Dictionary Resources\Dictionary.xml")
    Dictionary.Root.Elements("Word").Atrribute"ID"(IDChosen).Value = DateTime.Now
    Dictionary.Save("Dictionary Resources\Dictionary.xml")
    End Sub
Sub Timestamp(idChosen As String)
    Dim doc As XDocument = XDocument.Load("Dictionary Resources\Dictionary.xml")
    Dim word As XElement = doc.<Root>.<Word>.FirstOrDefault(Function(x) x.@ID = idChosen)
    If word IsNot Nothing Then
        word.<Timestamp>.First().SetValue(Date.Now)
        doc.Save("Dictionary Resources\Dictionary.xml")
    End If
End Sub
Sub Timestamp(idChosen As String)
    Dim doc As XDocument = XDocument.Load("Dictionary Resources\Dictionary.xml")
    Dim timeStamp As XElement = doc.XPathSelectElement("/Root/Word[@ID='" & idChosen & "']/Timestamp")
    If timeStamp IsNot Nothing Then
        timeStamp.SetValue(Date.Now)
        doc.Save("Dictionary Resources\Dictionary.xml")
    End If
End Sub
Sub Timestamp(idChosen As String)
    Dim doc As XDocument = XDocument.Load("Dictionary Resources\Dictionary.xml")
    Dim t As XElement = _
        (
        From word In doc.<Root>.<Word> 
        Where word.@ID = idChosen 
        Select word.<Timestamp>.FirstOrDefault()
        ).FirstOrDefault()
    If t IsNot Nothing Then
        t.SetValue(Date.Now)
        doc.Save("Dictionary Resources\Dictionary.xml")
    End If
End Sub
Sub Timestamp(idChosen As String)
    Dim doc As XDocument = XDocument.Load("Dictionary Resources\Dictionary.xml")
    Dim t As XElement = _
        (
        From word In doc.Root.Elements("Word") 
        Where word.Attributes("ID").Any(Function(x) x.Value = idChosen) 
        Select word.Element("Timestamp")
        ).FirstOrDefault()
    If t IsNot Nothing Then
        t.SetValue(Date.Now)
        doc.Save("Dictionary Resources\Dictionary.xml")
    End If
End Sub
Dim word As XElement = doc...<Word>.FirstOrDefault(Function(x) x.@ID = idChosen)
或者,如果您不关心实际的XML文档结构,只想找到具有该ID的第一个
Word
元素,而不管它位于文档树中的何处,您可以这样选择元素:

    Sub Timestamp(ByVal IDChosen As String)
    Dim Dictionary As XDocument = XDocument.Load("Dictionary Resources\Dictionary.xml")
    Dictionary.Root.Elements("Word").Atrribute"ID"(IDChosen).Value = DateTime.Now
    Dictionary.Save("Dictionary Resources\Dictionary.xml")
    End Sub
Sub Timestamp(idChosen As String)
    Dim doc As XDocument = XDocument.Load("Dictionary Resources\Dictionary.xml")
    Dim word As XElement = doc.<Root>.<Word>.FirstOrDefault(Function(x) x.@ID = idChosen)
    If word IsNot Nothing Then
        word.<Timestamp>.First().SetValue(Date.Now)
        doc.Save("Dictionary Resources\Dictionary.xml")
    End If
End Sub
Sub Timestamp(idChosen As String)
    Dim doc As XDocument = XDocument.Load("Dictionary Resources\Dictionary.xml")
    Dim timeStamp As XElement = doc.XPathSelectElement("/Root/Word[@ID='" & idChosen & "']/Timestamp")
    If timeStamp IsNot Nothing Then
        timeStamp.SetValue(Date.Now)
        doc.Save("Dictionary Resources\Dictionary.xml")
    End If
End Sub
Sub Timestamp(idChosen As String)
    Dim doc As XDocument = XDocument.Load("Dictionary Resources\Dictionary.xml")
    Dim t As XElement = _
        (
        From word In doc.<Root>.<Word> 
        Where word.@ID = idChosen 
        Select word.<Timestamp>.FirstOrDefault()
        ).FirstOrDefault()
    If t IsNot Nothing Then
        t.SetValue(Date.Now)
        doc.Save("Dictionary Resources\Dictionary.xml")
    End If
End Sub
Sub Timestamp(idChosen As String)
    Dim doc As XDocument = XDocument.Load("Dictionary Resources\Dictionary.xml")
    Dim t As XElement = _
        (
        From word In doc.Root.Elements("Word") 
        Where word.Attributes("ID").Any(Function(x) x.Value = idChosen) 
        Select word.Element("Timestamp")
        ).FirstOrDefault()
    If t IsNot Nothing Then
        t.SetValue(Date.Now)
        doc.Save("Dictionary Resources\Dictionary.xml")
    End If
End Sub
Dim word As XElement = doc...<Word>.FirstOrDefault(Function(x) x.@ID = idChosen)
或:


就我个人而言,我建议使用XPath方法来选择节点,因为它简短、简单、易于阅读,而且它使用的是行业标准的查询语言,而不是Microsoft专有的LINQ技术,但这只是我的想法:)

有多种方法可以做到这一点。您可以将
@
语法与
XDocument
对象一起使用,向下钻取到所需的元素,如下所示:

    Sub Timestamp(ByVal IDChosen As String)
    Dim Dictionary As XDocument = XDocument.Load("Dictionary Resources\Dictionary.xml")
    Dictionary.Root.Elements("Word").Atrribute"ID"(IDChosen).Value = DateTime.Now
    Dictionary.Save("Dictionary Resources\Dictionary.xml")
    End Sub
Sub Timestamp(idChosen As String)
    Dim doc As XDocument = XDocument.Load("Dictionary Resources\Dictionary.xml")
    Dim word As XElement = doc.<Root>.<Word>.FirstOrDefault(Function(x) x.@ID = idChosen)
    If word IsNot Nothing Then
        word.<Timestamp>.First().SetValue(Date.Now)
        doc.Save("Dictionary Resources\Dictionary.xml")
    End If
End Sub
Sub Timestamp(idChosen As String)
    Dim doc As XDocument = XDocument.Load("Dictionary Resources\Dictionary.xml")
    Dim timeStamp As XElement = doc.XPathSelectElement("/Root/Word[@ID='" & idChosen & "']/Timestamp")
    If timeStamp IsNot Nothing Then
        timeStamp.SetValue(Date.Now)
        doc.Save("Dictionary Resources\Dictionary.xml")
    End If
End Sub
Sub Timestamp(idChosen As String)
    Dim doc As XDocument = XDocument.Load("Dictionary Resources\Dictionary.xml")
    Dim t As XElement = _
        (
        From word In doc.<Root>.<Word> 
        Where word.@ID = idChosen 
        Select word.<Timestamp>.FirstOrDefault()
        ).FirstOrDefault()
    If t IsNot Nothing Then
        t.SetValue(Date.Now)
        doc.Save("Dictionary Resources\Dictionary.xml")
    End If
End Sub
Sub Timestamp(idChosen As String)
    Dim doc As XDocument = XDocument.Load("Dictionary Resources\Dictionary.xml")
    Dim t As XElement = _
        (
        From word In doc.Root.Elements("Word") 
        Where word.Attributes("ID").Any(Function(x) x.Value = idChosen) 
        Select word.Element("Timestamp")
        ).FirstOrDefault()
    If t IsNot Nothing Then
        t.SetValue(Date.Now)
        doc.Save("Dictionary Resources\Dictionary.xml")
    End If
End Sub
Dim word As XElement = doc...<Word>.FirstOrDefault(Function(x) x.@ID = idChosen)
或者,可以使用查询语法选择元素,如下所示:

    Sub Timestamp(ByVal IDChosen As String)
    Dim Dictionary As XDocument = XDocument.Load("Dictionary Resources\Dictionary.xml")
    Dictionary.Root.Elements("Word").Atrribute"ID"(IDChosen).Value = DateTime.Now
    Dictionary.Save("Dictionary Resources\Dictionary.xml")
    End Sub
Sub Timestamp(idChosen As String)
    Dim doc As XDocument = XDocument.Load("Dictionary Resources\Dictionary.xml")
    Dim word As XElement = doc.<Root>.<Word>.FirstOrDefault(Function(x) x.@ID = idChosen)
    If word IsNot Nothing Then
        word.<Timestamp>.First().SetValue(Date.Now)
        doc.Save("Dictionary Resources\Dictionary.xml")
    End If
End Sub
Sub Timestamp(idChosen As String)
    Dim doc As XDocument = XDocument.Load("Dictionary Resources\Dictionary.xml")
    Dim timeStamp As XElement = doc.XPathSelectElement("/Root/Word[@ID='" & idChosen & "']/Timestamp")
    If timeStamp IsNot Nothing Then
        timeStamp.SetValue(Date.Now)
        doc.Save("Dictionary Resources\Dictionary.xml")
    End If
End Sub
Sub Timestamp(idChosen As String)
    Dim doc As XDocument = XDocument.Load("Dictionary Resources\Dictionary.xml")
    Dim t As XElement = _
        (
        From word In doc.<Root>.<Word> 
        Where word.@ID = idChosen 
        Select word.<Timestamp>.FirstOrDefault()
        ).FirstOrDefault()
    If t IsNot Nothing Then
        t.SetValue(Date.Now)
        doc.Save("Dictionary Resources\Dictionary.xml")
    End If
End Sub
Sub Timestamp(idChosen As String)
    Dim doc As XDocument = XDocument.Load("Dictionary Resources\Dictionary.xml")
    Dim t As XElement = _
        (
        From word In doc.Root.Elements("Word") 
        Where word.Attributes("ID").Any(Function(x) x.Value = idChosen) 
        Select word.Element("Timestamp")
        ).FirstOrDefault()
    If t IsNot Nothing Then
        t.SetValue(Date.Now)
        doc.Save("Dictionary Resources\Dictionary.xml")
    End If
End Sub
Dim word As XElement = doc...<Word>.FirstOrDefault(Function(x) x.@ID = idChosen)
或者,如果您不关心实际的XML文档结构,只想找到具有该ID的第一个
Word
元素,而不管它位于文档树中的何处,您可以这样选择元素:

    Sub Timestamp(ByVal IDChosen As String)
    Dim Dictionary As XDocument = XDocument.Load("Dictionary Resources\Dictionary.xml")
    Dictionary.Root.Elements("Word").Atrribute"ID"(IDChosen).Value = DateTime.Now
    Dictionary.Save("Dictionary Resources\Dictionary.xml")
    End Sub
Sub Timestamp(idChosen As String)
    Dim doc As XDocument = XDocument.Load("Dictionary Resources\Dictionary.xml")
    Dim word As XElement = doc.<Root>.<Word>.FirstOrDefault(Function(x) x.@ID = idChosen)
    If word IsNot Nothing Then
        word.<Timestamp>.First().SetValue(Date.Now)
        doc.Save("Dictionary Resources\Dictionary.xml")
    End If
End Sub
Sub Timestamp(idChosen As String)
    Dim doc As XDocument = XDocument.Load("Dictionary Resources\Dictionary.xml")
    Dim timeStamp As XElement = doc.XPathSelectElement("/Root/Word[@ID='" & idChosen & "']/Timestamp")
    If timeStamp IsNot Nothing Then
        timeStamp.SetValue(Date.Now)
        doc.Save("Dictionary Resources\Dictionary.xml")
    End If
End Sub
Sub Timestamp(idChosen As String)
    Dim doc As XDocument = XDocument.Load("Dictionary Resources\Dictionary.xml")
    Dim t As XElement = _
        (
        From word In doc.<Root>.<Word> 
        Where word.@ID = idChosen 
        Select word.<Timestamp>.FirstOrDefault()
        ).FirstOrDefault()
    If t IsNot Nothing Then
        t.SetValue(Date.Now)
        doc.Save("Dictionary Resources\Dictionary.xml")
    End If
End Sub
Sub Timestamp(idChosen As String)
    Dim doc As XDocument = XDocument.Load("Dictionary Resources\Dictionary.xml")
    Dim t As XElement = _
        (
        From word In doc.Root.Elements("Word") 
        Where word.Attributes("ID").Any(Function(x) x.Value = idChosen) 
        Select word.Element("Timestamp")
        ).FirstOrDefault()
    If t IsNot Nothing Then
        t.SetValue(Date.Now)
        doc.Save("Dictionary Resources\Dictionary.xml")
    End If
End Sub
Dim word As XElement = doc...<Word>.FirstOrDefault(Function(x) x.@ID = idChosen)
或:


就个人而言,我建议使用XPath方法来选择节点,因为它简短、简单、易于阅读,而且它使用的是行业标准的查询语言,而不是Microsoft专有的LINQ技术,但这只是我的想法:)

根据示例XML中的格式,不清楚,如果您正在查找
/Root/Word/Word
,或
/Root/Word
。抱歉,刚刚在post中编辑了xml,它应该是/Root/Word。您正在尝试设置哪个元素的值?是要设置为当前时间的
/Root/Word/Timestamp
元素吗?根据您的psuedo代码,这并不完全清楚。是的,这是我试图将当前时间设置为的/Root/Word/Timestamp。根据示例XML中的格式,如果您正在查找
/Root/Word/Word
,或
/Root/Word
,则不清楚。抱歉,刚刚在post中编辑了XML,它应该是/Root/Word。您要设置哪个元素的值?是要设置为当前时间的
/Root/Word/Timestamp
元素吗?根据您的psuedo代码,这还不完全清楚。是的,我正试图将当前时间设置为/Root/Word/Timestamp。我对@or函数都不熟悉。我所写的其余代码都是使用诸如Dictionary.Root.Elements(“Word”)等引用编写的。是否可以使用以下内容来实现它
Dim t As XElement=(来自Dictionary.Root.Elements(“Word”)中的x,其中x.Attribute(“ID”)=idselected选择x.Elements(“Timestamp”)
感谢您的回答Steven,示例非常好。我同意您的XPath建议,我发现它更容易理解。我对@or函数都不熟悉。我所写的其余代码都是使用诸如Dictionary.Root.Elements(“Word”)等引用编写的。是否可以使用以下内容来实现它
Dim t As XElement=(来自Dictionary.Root.Elements(“Word”)中的x,其中x.Attribute(“ID”)=idselected选择x.Elements(“Timestamp”)
感谢您的回答Steven,示例非常好。我同意您的XPath建议,我发现它更容易理解。