如何使用c#在xml文件中更改子节点的文本值

时间:2017-01-01 15:50:23

标签: c# xml

我在尝试之前尝试过寻找解决方案,但我还没找到我想要的东西。

<bookstore>


<book category="cooking">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
  </book>
  <book category="children">
    <title lang="en">Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
  </book>

在上面的示例中,假设我想将文本vaule“Harry Potter”更改为其他内容,最简单的方法是什么?

2 个答案:

答案 0 :(得分:0)

XDocumentLINQ to XML为您提供了一种非常有表现力的方式。

// Parse our xml into an XDocument, which is a tree of nodes and collections of XElements, which represent xml elements
var xdoc = XDocument.Parse(
@"<bookstore>
  <book category=""cooking"">
     <title lang=""en"">Everyday Italian</title>
     <author>Giada De Laurentiis</author>
     <year>2005</year>
     <price>30.00</price>
  </book>
  <book category=""children"">
      <title lang=""en"">Harry Potter</title>
      <author>J K. Rowling</author>
      <year>2005</year>
      <price>29.99</price>
  </book>
  </bookstore>");

var book = xdoc
           // Start with the root node (bookstore)
           .Root
           // Take the child elements (books)
           .Elements()
           // Take 1, and only 1 book element such that it contains a title element with the value "Harry Potter"
           .Single(x => x.Element("title").Value == "Harry Potter");

// Set this books title element to the new title
book.Element("title").SetValue("NEW TITLE");
// Any changes we make alters the underlying XDocument, so we can print the final result.
Console.Write(xdoc.ToString());

有关详细信息,请阅读C# 6.0 in a Nutshell的第10章。

答案 1 :(得分:0)

这是实现目标的另一种方式。代码比Stuart提供的代码多得多。但我更喜欢这种方法,因为这是类型安全的,并且会阻止您在数字字段中输入文本。

import tkinter as tk
from PIL import Image, ImageTk
import random

class MainApplication(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)
        self.parent = parent

        # creating a master frame within root
        mainFrame = tk.Frame(root)
        mainFrame.place(relwidth=1, relheight = 1, x = 0, y = 0)
        mainFrame.config(bg = "red")

        # the blue frame in the middle of the screen
        imageDivider = tk.Frame(mainFrame)
        imageDivider.config(bg = "blue")
        imageDivider.place(relx = 0, rely = 0.3, relwidth = 1, relheight = 0.4)

        # the custom image attempt
        testImage1 = CustomImage(imageDivider, "images/face.png")
        testImage1.place(x=0,y=0)

        # A standard image is inserted into the blue frame
        imageTemp = Image.open("images/face.png")
        picPI     = ImageTk.PhotoImage(imageTemp) #Pic PhotoImaged
        imgTest = tk.Label(imageDivider)
        imgTest.config(bg = "#111111", image = picPI, borderwidth = 0) # borderwidth must be 0 to prevent padding
        imgTest.image = picPI #keep a reference!
        imgTest.place(x=0,y=0)

# A custom image wrapper class
class CustomImage(tk.Label):
    def __init__(self, parent, imagePath, *args, **kwargs):
        tk.Label.__init__(self, *args, **kwargs)
        self.parent = parent
        imageTemp = Image.open(imagePath)
        picPI     = ImageTk.PhotoImage(imageTemp) #Pic PhotoImaged
        self.configure(bg = "#111111", image = picPI, borderwidth = 0) # borderwidth must be 0 to prevent padding
        self.image = picPI #keep a reference!

# standard stuff
if __name__ == "__main__":
    root = tk.Tk()
    root.config(background="black")
    root.geometry("500x500")
    root.resizable(width=True, height=True)
    MainApplication(root).pack(side="top", fill="both", expand=True)
    root.mainloop()

希望这有帮助。