简体   繁体   中英

How to read o:xml with powershell?

I have following xml file but don't know how to read it with Powershell, Anyone can help with? Thanks!

I need to get the url value from Powershell.

<o:OfficeConfig xmlns:o="urn:xxx:xxx:xxx">
<o:services>
<o:service o:name="xxxx">
<o:url>https://xxx.xxx</o:url>
</o:service>
</o:services>
</o:OfficeConfig>

Thanks in advance!

You can take advantage of the fact that PowerShell's convenient, property-based adaptation of the XML DOM essentially ignores namespaces, allowing to you simply drill down to the element of interest by the unqualified element names:

([xml] (Get-Content -Raw file.xml)).OfficeConfig.services.service.url

By contrast, the XPath-based Select-Xml cmdlet is namespace-aware, and therefore requires explicit namespace handling - or a workaround via the local-name() function, as shown in Mathias R. Jessen's answer .

If you want to use proper namespace handling - which is ultimately more robust, but not always necessary - use the following:

(
  Select-Xml '//o:url' file.xml -Namespace @{ o='urn:schemas-microsoft-com:office:office' }
).Node.InnerText
  • Note the need to pass a hashtable ( @{ ... } ) that declares the namespace prefixes and URLs used, which is the prerequisite for being able to use the prefixes ( o: , in this case) in the XPath query.

    • The prefix names need not match the ones in the original, as long as they're consistent with the -Namespace argument and are mapped to the original URLs.
  • Select-Xml returns wrapper objects around the matched System.Xml.XmlNode instances, so .Node is required to access the latter, and .InnerText then returns the node's text content.

    • As an aside: This need to access .Node is inconvenient, as the typical use case is to care about the XmlNode only; GitHub suggestion #13669 seeks to ease the pain via a
      -Raw switch that returs the XmlNode instances directly.

You could use Select-Xml :

$rawXml = @'
<o:OfficeConfig xmlns:o="urn:schemas-microsoft-com:office:office">
<o:services>
<o:service o:name="GetFederationProvider">
<o:url>https://odc.officeapps.live.com/odc/emailhrd/getfederationprovider</o:url>
</o:service>
</o:services>
</o:OfficeConfig>
'@ 

$urlNode = $rawXml |Select-Xml -XPath '//*[local-name() = "url"]' |Select -Expand Node
$url = $urlNode.innerText

$url will now contain the string "https://odc.officeapps.live.com/odc/emailhrd/getfederationprovider"

Since you are returning json, you can just convert from json to a PowerShell object:

$configServiceUrl = "https://officeclient.microsoft.com/config16processed?rs=en-us&build=16.0.7612"
$headers = @{'Accept' = 'application/json'}
$getFederationProviderEndpoint = Invoke-WebRequest -Uri "$($configServiceUrl)&services=GetFederationProvider" -Headers $headers -Method GET

$obj = $getFederationProviderEndpoint.Content | ConvertFrom-Json
$obj.'o:officeconfig'.'o:services'.'o:service'.'o:url'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM