.NET Framework - Help adding XML element at specified "path"
Asked By Adrian
01-Feb-10 04:38 AM
Say I have the following XML in a variable called "$myxml"...
...How do I add (an set the value of) an element at, say,
Many Thanks,
=Adrian=
Node.AppendChild
(1)
XPath
(1)
Firstlevel01
(1)
Firstlevel02
(1)
Item03
(1)
Myxml
(1)
XML
(1)
Exp
(1)
stej replied to Adrian
Try this:
$xml =3D [xml]@"
$i =3D $xml.CreateElement('item03')
$i.InnerText =3D 'this is test'
$node =3D Select-Xml -Xml $xml -XPath /root/firstlevel02 | select -exp
Node
$node.AppendChild($i) > $null
$xml | Format-Custom
$xml.root.firstlevel02

why is count(xpath) ! = xpath[last()] ? .NET Framework While writing a template, I came across a situation in Microsoft's evaluation of my XSLT where, for a given XPath expression, count() of that nodeset was not equal to last() of that nodeset. I can last()]" / > [position() = 1]" / > [position() = last()]" / > The XML output from this transform is Note that the XPath / root / Students / row / Name yielded a nodeset of two names, as shown by the value Xsl Discussions ASP.NET (1) TagName (1) Jeni (1) Confuses (1) Jfrensen (1) Nodeset (1) Xpath (1) XSLT (1) That's because Name elements aren't siblings. Each Name element is contains single Name element, so last() = 1. last() function returns context node-set size. During XPath expression evaluation context node-set is changing by each location step and predicate. You need the use of the predicate filter seems to change the order of evaluation of the XPath expression. That is, select = " / root / Students / row / Name" evaluates to a nodeset of two Names Students / row / Name)" evaluates to the number 2. Given that, I had assumed that the XPath expression select = " / root / Students / row / Name[position() = last()]" would be evaluated from left to right
selectsinglenode of namespace attribute .NET Framework I'm loading an xml and doing selectSingleNode( / / *[xpath]) xpath is with namespace attribute. var xmlStr = "<R AB:I = '1' xmlns:AB = 'urn:MynameSpace'> <I new ActiveXObject("MSXML2.DOMDocument.4.0"); abc.async = false abc.loadXML(xmlStr) abc.setProperty("SelectionLanguage", "XPath"); abc.setProperty("SelectionNamespaces", "xmlns:AB = 'urn:MynameSpace'"); alert(abc.documentElement.xml) / / xmml i coming proper question keywords: selectsinglenode, of, namespace, attribute description: I'm loading an xml and doing selectSingleNode( / / *[xpath]) xpath is with namespace attribute. var xmlStr = R AB:I = '1' xmlns:AB = 'urn:MynameSpace'I
if (root ! = null) { node = root.LastChild; root.AppendChild(xd.CreateElement("med")); node = root.SelectSingleNode("med"); node.AppendChild(xd.CreateElement("ID")).InnerText = "555"; node.AppendChild(xd.CreateElement("Datum")).InnerText = System.DateTime.Now.ToString(); node.AppendChild(xd.CreateElement("Filename")).InnerText = } xd.Save(@"d: \ mediation.xml"); } Er schreibt die Elemente aber immer SelectSingleNode an der Stelle? - -- -- -- -- -- -- --fixed if block - -- - root.AppendChild(xd.CreateElement("med")); node = root.LastChild; node.AppendChild(xd.CreateElement("ID")).InnerText = "555"; node.AppendChild(xd.CreateElement("Datum")).InnerText = System.DateTime.Now.ToString