Foreach on XML返回字符串

时间:2018-01-16 14:59:29

标签: xml powershell

我正在尝试通过PowerShell读取XML文件。

示例XML:

import tkinter as tk

data = {
    "easy": [
         [8,0,0,0,4,0,6,7,0],
         [0,3,0,6,7,0,5,0,8],
         [0,7,2,0,0,0,0,3,4],
         [1,0,3,7,0,4,8,0,5],
         [0,6,5,0,1,8,4,9,0],
         [0,4,8,0,5,6,0,2,0],
         [5,9,0,4,2,0,0,0,0],
         [2,0,7,0,3,0,1,0,6],
         [3,0,4,0,6,0,2,0,9],
    ],

    "hard": [
        [0,8,0,3,0,5,0,1,0],
        [1,0,0,0,0,0,0,0,0],
        [0,0,3,2,0,0,6,0,0],
        [0,0,7,0,0,0,0,9,8],
        [8,0,2,0,5,0,4,0,3],
        [4,1,0,0,0,0,2,0,0],
        [0,0,4,0,0,8,9,0,0],
        [0,0,0,0,0,0,0,0,1],
        [0,9,0,7,0,6,0,8,0],
    ],
}    

solved = {
    "easy": [
         [8,5,1,9,4,3,6,7,2],
         [4,3,9,6,7,2,5,1,8],
         [6,7,2,1,8,5,9,3,4],
         [1,2,3,7,9,4,8,6,5],
         [7,6,5,2,1,8,4,9,3],
         [9,4,8,3,5,6,7,2,1],
         [5,9,6,4,2,1,3,8,7],
         [2,8,7,5,3,9,1,4,6],
         [3,1,4,8,6,7,2,5,9],
    ],
    "hard": [
        [2,8,6,3,4,5,7,1,9],
        [1,4,5,6,9,7,8,3,2],
        [9,7,3,2,8,1,6,4,5],
        [3,5,7,4,6,2,1,9,8],
        [8,6,2,1,5,9,4,7,3],
        [4,1,9,8,7,3,2,5,6],
        [6,3,4,5,1,8,9,2,7],
        [7,2,8,9,3,4,5,6,1],
        [5,9,1,7,2,6,3,8,4],
    ],
}

def left_click(widget):
    ''' process button click '''

    # get next value (modulo 10)
    widget.value = (widget.value+1) % 10

    # if zero then put empty string
    if widget.value == 0:
        widget['text'] = ''
    else:
        widget['text'] = widget.value

def create_empty_board(master):
    ''' create board with empty buttons '''

    # create black lines
    master['bg'] = '#444'
    master.columnconfigure(3, minsize=3)
    master.columnconfigure(7, minsize=3)
    master.rowconfigure(3, minsize=3)
    master.rowconfigure(7, minsize=3)

    all_buttons = []

    for r in range(11):
        # skip rows to create black lines
        if r in (3, 7):
            continue
        row_buttons = []
        for c in range(11):
            # skip columns to create black lines
            if c in (3, 7):
                continue
            b = tk.Button(master, text='-')
            b.grid(row=r, column=c, sticky='we')
            row_buttons.append(b)
        all_buttons.append(row_buttons)

    return all_buttons

def reset_board(all_buttons, local_data):
    ''' reset buttons on board without creating new buttons '''

    for row_buttons, row_sudoku in zip(all_buttons, local_data):
        for b, v in zip(row_buttons,row_sudoku):
            # create unstandard variable to keep current value on button
            b.value = v

            if v == 0:
                b['text'] = ' '
                b['command'] = lambda but=b:left_click(but)
                #b['fg'] = 'black'
                b['bg'] = '#ddd'
                b['state'] = 'normal'
                b['disabledforeground'] = 'black'
            else:
                b['text'] = v
                b['command'] = None
                #b['fg'] = 'blue'
                b['bg'] = '#cce'
                b['state'] = 'disabled'
                b['disabledforeground'] = 'blue'

def check_buttons():
    ''' compare buttons with `solved` '''

    local_data = solved[difficalty]

    for r, (row_buttons, row_sudoku) in enumerate(zip(all_buttons, local_data), 1):
        row_ok = True
        for c, (b, v) in enumerate(zip(row_buttons,row_sudoku), 1):
            if b.value != v:
                print('wrong row: {}, col: {}, button: {}, expected: {}'.format(r, c, b.value, v))
                row_ok = False
        if row_ok:
            print('OK row:', r)

def change_difficalty(level):
    ''' change difficalty and reset board '''

    global difficalty
    difficalty = level

    reset_board(all_buttons, data[difficalty])

# --- main ---

difficalty = "easy"

root = tk.Tk()

board = tk.Frame(root)
board.pack()

all_buttons = create_empty_board(board)

reset_board(all_buttons, data[difficalty])

b = tk.Button(root, text="Easy", command=lambda:change_difficalty("easy"))
b.pack()

b = tk.Button(root, text="Hard", command=lambda:change_difficalty("hard"))
b.pack()

b = tk.Button(root, text="Check board", command=check_buttons)
b.pack()

root.mainloop()

示例代码:

<branches>
    <branch>refs/heads/master</branch>
    <branch Version="6.0.7">develop/4.0</branch>
</branches>

问题是属性$xml = [xml](Get-Content $xmlfile) foreach ($branch in $xml.branches.branch) { $version = $branch.Version } 是可选的,PowerShell将第一次出现的Version解释为字符串。第二次出现工作正常。我需要它们都是XmlElement。我已经尝试将branch转换为XmlElements数组而没有成功。

如何转换为XmlElement?

1 个答案:

答案 0 :(得分:1)

SelectSingleNode()使用XPath expression而非点访问:

$version = $xml.SelectSingleNode('//branches/branch/@Version').'#text'