Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8838045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:53:17+00:00 2026-06-14T09:53:17+00:00

I’m trying to manipulate a xml file from sharepoint document library with powershell. Unfortunately

  • 0

I’m trying to manipulate a xml file from sharepoint document library with powershell. Unfortunately I cannot open it and cast it to xml. 🙁 I have same file in the document library and locally on hard drive. When I open it from hard drive everything is fine. When I open it from Sharepoint Document Library the cast to xml fails because of an extra character in the beginning of the file. I binary compared the result from $splistitem.File.OpenBinary() and from get-content C:….\file.xml and its the same. The problem is with getting the string from bytes. I tried all available encodings, but nothing worked. Here is my code:

   # Add SharePoint snapin if needed
    if ((Get-PSSnapin -Name Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue)     -eq $null)
    {
        Add-PSSnapin Microsoft.SharePoint.Powershell
    }

...
    $splistitem = ...
...

    $encode = New-Object System.Text.UTF8Encoding

    [xml]$xmlFromSharepoint = $encode.GetString($splistitem.File.OpenBinary()) #throws exception
    [xml]$xmlFromFile = get-content C:\....\file.xml #works fine

    $web.Dispose()

    Write-Host "Press any key to close ..."
    $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

The exception thrown is with message:

        Cannot convert value "?<?xml version="1.0" encoding="utf-8"?> 
..." to type "System.Xml.XmlDocument". Error: "Data at the
    root level is invalid. Line 1, position 1."
    At C:\aaa.ps1:14 char:24
    + [xml]$xmlFromSharepoint <<<<  = $encode.GetString($splistitem.File.OpenBinary
    ())
        + CategoryInfo          : MetadataError: (:) [], ArgumentTransformationMet
       adataException
        + FullyQualifiedErrorId : RuntimeException
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T09:53:18+00:00Added an answer on June 14, 2026 at 9:53 am

    I’m answering myself. 🙂

    The character infront of file is a byte order mark for UTF8 encoding. I found that I can load the xml file directly from sharepoint stream. This sloves the problem. My goal was to chages the connection string for Reporting Services datasource in sharepoint library. The .rsds file is a normal xml file which holds the connection string. Here is the code to modify it:

    # Add SharePoint snapin if needed
    if ((Get-PSSnapin -Name Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue)     -eq $null)
    {
        Add-PSSnapin Microsoft.SharePoint.Powershell
    }
    
    #Configure Connection strings
    $web = Get-SPWeb "http://localhost/c1"
    $reportsList = $web.Lists | Where-Object { $_.Title -eq "Reports" }
    $dataSource = $reportsList.Items | Where-Object { $_.Name -eq "Datasource.rsds" }
    $dataSource.File.CheckOut();
    $xml = New-Object System.Xml.XmlDocument
    $stream = $dataSource.File.OpenBinaryStream();
    $xml.Load($stream)
    $xml.DataSourceDefinition.ConnectString = "test"
    $stream.Position = 0;
    $stream.SetLength(0);
    $xml.Save($stream);
    $dataSource.File.SaveBinary($stream)
    $dataSource.File.CheckIn("");
    $web.Dispose()
    
    Write-Host "Press any key to continue ..."
    $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
    

    Actually I realized that the above code doesn’t work. To modify reports I have to use the ReportServer web service. Here is now the working code:

    function ConfigureConnectionString
    {
    param($webUrl, $connectionString)
        $reportServerURI = "http://$serverUrl/ReportServer"
        $ReportPathWildCard = "/";
        $NameSharedSchedule="NeverExpireCache";
    
        $reportServerURI2010 = "$reportServerURI/ReportService2010.asmx?WSDL"
        $RS = New-WebServiceProxy -Class 'RS' -NameSpace 'RS' -Uri $reportServerURI2010 -UseDefaultCredential
        $dataSources = $RS.ListChildren($ReportPathWildCard,$true) | Where-Object {$_.TypeName -eq "DataSource" -and $_.Path -like "*$webUrl*"}
        foreach($ds in $dataSources) {
            $dsDefinition = $RS.GetDataSourceContents($ds.Path)
    
            $dsDefinition.CredentialRetrieval = "Store"
            $dsDefinition.Enabled = "TRUE";
            $dsDefinition.UserName = "domain\user";
            $dsDefinition.Password = "Password";
            $dsDefinition.ConnectString = $connectionString;
    
            #Update the DataSource with this new content
            $RS.SetDataSourceContents($ds.Path, $dsDefinition);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
I am trying to render a haml file in a javascript response like so:
In my XML file chapters tag has more chapter tag.i need to display chapters
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have a text area in my form which accepts all possible characters from
I have a view passing on information from a database: def serve_article(request, id): served_article
I have a reasonable size flat file database of text documents mostly saved in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.