Powershell可以';设置xmlelement的内部文本
我试图制作一个脚本来更新sharepoint中的web部件,我找到了一些示例,但是我有一个xml问题,我必须将它传递给web部件 请注意,我使用的powershell是1.0Powershell可以';设置xmlelement的内部文本,xml,powershell,sharepoint-2007,Xml,Powershell,Sharepoint 2007,我试图制作一个脚本来更新sharepoint中的web部件,我找到了一些示例,但是我有一个xml问题,我必须将它传递给web部件 请注意,我使用的powershell是1.0 $xmlDoc = New-Object xml; $newXmlElement = $xmlDoc.CreateElement("HtmlContent"); $newXmlElement.InnerText="SomeValue"; 所以这段脚本在中断时,我得到一个错误,
$xmlDoc = New-Object xml;
$newXmlElement = $xmlDoc.CreateElement("HtmlContent");
$newXmlElement.InnerText="SomeValue";
所以这段脚本在中断时,我得到一个错误,它说:
在此对象上找不到属性“InnerText”;确保
存在并可设置
我真的不知道为什么它不起作用,有人知道吗
我尝试在Windows Powershell comman行中执行这几行,当我尝试设置innertext时,它会向我抛出一条红色错误消息。我终于找到了一种方法,似乎在Powershell v1.0中,object System.Xml.XmlElement没有innertext等属性,因此我的方法如下:
$xmlDoc=New-Object System.Xml.XmlDocument;
$xmlElement=$xmlDoc.CreateElement("HtmlElement");
$xmlText = $xmlDoc.CreateTextNode($cewpNewContent)
$xmlElement.AppendChild($xmlText);
我希望这对其他人有用。尝试使用点表示法为现有元素赋值时出现的错误是因为点表示法访问您所在的
XmlNode
的xmlement
XmlElement
没有InnerText
,其中XmlNode
有
关于如何使用InnerText创建和赋值的示例
$xmlDoc = New-Object xml;
$newXmlElement = $xmlDoc.CreateNode("element", "HtmlContent", "")
$newXmlElement.InnerText = "SomeValue"
$secondXmlElement = $xmlDoc.CreateNode("element", "HtmlContentChild", "")
$secondXmlElement.InnerText = "NewValue"
$newXmlElement.AppendChild($secondXmlElement)
$xmlDoc.AppendChild($newXmlElement)
循环和节点访问示例
foreach($node in $nodes) {
if ($node.element -ne $null) {
$node.element.InnerText = "this will throw error" # generates the error
$node.element = "this is correct" # Correct way to add InnerText
}
else {
$elementToAdd = $xmlDoc.CreateNode("element", "element", "")
$elementToAdd.InnerText = "This is correct"
$node.AppendChild($elementToAdd)
}
}
使用指定的节点类型、名称和NamespaceURI(在您的示例中,NamespaceURI为null)创建XmlNode
将指定节点添加到此节点的子节点列表的末尾