将字符串编码为XML

时间:2017-05-14 23:54:00

标签: xml web-services powershell powershell-v3.0

我有一个我需要发布到Web服务的XML文档。下面的示例文件:

Sub Main()
    Dim input As String
    Dim str(5) As String
    Dim tempstr As String
    Dim temp As Char
    Dim temp2 As Char
    Dim l As Integer
    Console.WriteLine("Enter the string: ")
    input = Console.ReadLine()
    l = Len(input)
    For z As Integer = 1 To 5
        For i As Integer = 1 To l
            temp = Mid(input, i, l)
            temp2 = Mid(input, i + 1, l)
            If Asc(temp) > 65 And Asc(temp) < 90 Then
                tempstr = temp
                If Asc(temp2) > 65 And Asc(temp2) < 90 Then
                    tempstr = temp
                Else
                    tempstr = tempstr & temp
                End If
            Else
                tempstr = tempstr & temp
            End If
        Next i
        str(z) = tempstr
    Next z
    For a As Integer = 1 To 5
        Console.WriteLine(str(a))
    Next a
    Console.ReadKey()
End Sub

我需要将此数据作为编码XML发布到Web服务。示例如下:

<Budgets>
    <Budgetline LocationName="Test Location 1" LocationCode="TL1"  Period="201705" Amount="1276.000000"/>
    <Budgetline LocationName="Test Location 2" LocationCode="TL2"  Period="201705" Amount="1530.050000"/> </Budgets>

我将文件作为字符串读取,并使用以下方法将其转换为XML:

&lt;Budgets&gt;
    &lt;Budgetline LocationName=&quot;Test Location 1&quot; LocationCode=&quot;TL1&quot;  Period=&quot;201705&quot; Amount=&quot;1276.000000&quot;/&gt;
    &lt;Budgetline LocationName=&quot;Test Location 2&quot; LocationCode=&quot;TL2&quot;  Period=&quot;201705&quot; Amount=&quot;1530.050000&quot;/&gt; &lt;/Budgets&gt;

然而,XML数据被一个对象元素[string]$budgetsfile = Get-Content "D:\Upload\TestData.xml" -Raw $xmlBudgets = ConvertTo-Xml $budgetsfile -As Stream -NoTypeInformation 包装,Web服务会为其返回错误。

有没有办法将数据编码为没有<Object><Objects> XMLdata </Object></Objects>标记的XML。或者我应该使用string.replace()来删除它们。

肥皂信封需要看起来像这样:

<Object><Objects>

2 个答案:

答案 0 :(得分:0)

您可以直接将数据转换为xml:

[xml]$xmlBudgets = Get-Content "D:\Upload\TestData.xml" -Raw

对其进行编码,您可以使用:

Add-Type -AssemblyName System.Web
#below command will create the encoded string and assign it to variable
$encoded = [System.Web.HttpUtility]::HtmlEncode($xmlBudgets.Innerxml)

对于肥皂信封,你可以使用它:

$head = @"
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://example.com/XMLSchema-instance" xmlns:xsd="http://example.com/2001/XMLSchema" xmlns:soap12="http://example.com/2003/05/soap-envelope">
  <soap12:Body>
    <upload xmlns="http://example.com">
      <authUser>$user</authUser>
      <authPass>$pass</authPass>
      <xmlData>
"@

$tail = @"
</xmlData>
    </upload>
  </soap12:Body>
</soap12:Envelope>
"@

$head + $encoded +$tail

答案 1 :(得分:0)

感谢大家的投入。解决方案是将XML文件作为字符串读取并使用Abhijith pk HtmlEncode建议对其进行编码,但是在字符串而不是XML节点上进行编码。

[string]$xmlBudgets = Get-Content "D:\Upload\TestData.xml" -Raw
Add-Type -AssemblyName System.Web
$encoded = [System.Web.HttpUtility]::HtmlEncode($budgetsfile)

肥皂信封看起来像这样。

[xml]$SOAP_upload = '<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://example.com/XMLSchema-instance" xmlns:xsd="http://example.com/2001/XMLSchema" xmlns:soap12="http://example.com/2003/05/soap-envelope">
  <soap12:Body>
    <upload xmlns="http://example.com">
      <authUser>$user</authUser>
      <authPass>$pass</authPass>
      <xmlData>'+$encoded+'</xmlData>
    </upload>
  </soap12:Body>
</soap12:Envelope>'