Przy pomocy PowerShell chcę dodać kilka podelementów do drzewa XML.
Znam ADD ONE element, wiem, aby dodać jeden lub kilka atrybutów, ale nie rozumiem, jak dodać kilka elementów.Dodawanie podelementów XML
Jednym ze sposobów może być write a sub-XML tree as text
Ale nie mogę użyć tej metody, ponieważ elementy nie są dodawane jednocześnie.
Aby dodać jeden element, to zrobić:
[xml]$xml = get-content $nomfichier
$newEl = $xml.CreateElement('my_element')
[void]$xml.root.AppendChild($newEl)
działa dobrze. To daje mi to drzewo XML:
$xml | fc
class XmlDocument
{
root =
class XmlElement
{
datas =
class XmlElement
{
array1 =
[
value1
value2
value3
]
}
my_element = <-- the element I just added
}
}
Teraz chcę dodać element podrzędny do "my_element". Używam podobną metodę:
$anotherEl = $xml.CreateElement('my_sub_element')
[void]$xml.root.my_element.AppendChild($anotherEl) <-- error because $xml.root.my_element is a string
[void]$newEl.AppendChild($anotherEl) <-- ok
$again = $xml.CreateElement('another_one')
[void]$newEl.AppendChild($again)
To daje to drzewo XML (ciowo wyświetlany):
my_element =
class XmlElement
{
my_sub_element =
another_one =
}
Są to atrybuty, nie podelementów.
podelementów będzie wyświetlany jako to:
my_element =
[
my_sub_element
another_one
]
Pytanie: Jak mogę dodać kilka mniejszych elementów, jeden na raz?
O wiele lepsze rozwiązanie niż to, co do tej pory znalazłem! I czystsze. Dzięki. –
To działa dobrze. Moim problemem jest to, że zapisuje wiele rzeczy na konsoli. Debugowanie pokazuje, że dzieje się to w tym wierszu: $ xmlSubElt.AppendChild ($ xmlSubText) , jeśli napiszę "> $ null" na końcu. Coś innego jest napisane Czy mogę to zablokować? – Tomi
Zamień '> $ null' na' | out-null ". – JPBlanc