1

I have an XML in powershell below:

[Xml]$MyXmlVariable = @"
<Mylist>
  <Item Number="1">
    <Name>"AMD Ryzen 5 3600x"</Name>
    <Type>"CPU"</Type>
    <Price>"$169.99"</Price>
    <Where>"Amazon"</Where>
    <Other>
      <ThreadCount>"12"</ThreadCount>
      <Cores>"6"</Cores>
    </Other>
  </Item>
</MyList>
"@

And it looks fne to me, but when I try to define it, I get the error

Cannot convert value "<Mylist>
  <Item Number="1">
    <Name>"AMD Ryzen 5 3600x"</Name>
    <Type>"CPU"</Type>
    <Price>".99"</Price>
    <Where>"Amazon"</Where>
    <Other>
      <ThreadCount>"12"</ThreadCount>
      <Cores>"6"</Cores>
    </Other>
  </Item>
</MyList>" to type "System.Xml.XmlDocument". Error: "The 'Mylist' start tag on line 1
position 2 does not match the end tag of 'MyList'. Line 12, position 3."
At line:1 char:1
+ [Xml]$MyXmlVariable = @"
+ ~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : MetadataError: (:) [], ArgumentTransformationMetadataExcepti
   on
    + FullyQualifiedErrorId : RuntimeException

AFAIK there doesn't seem to be an error but Powershell says otherwise. Please Help

5
  • XMLs are case sensitive. MyList and Mylist are different so that is where the error comes from. See stackoverflow.com/questions/35220463/… Commented Jun 22, 2020 at 22:09
  • Thank you, when I changed <Mylist> to <MyList> it works :P. Sorry for the stupid question... One more thing, if I have multiple items and I want the item where the Number value is equal to 1 what would I do? Commented Jun 22, 2020 at 22:15
  • Use ?/Where-Object, $MyXmlVariable.MyList.Item | ? {$_.Number -eq 1} you could do this with other values too, $MyXmlVariable.MyList.Item | ? {$_.Type -eq "CPU"} Commented Jun 22, 2020 at 22:17
  • Thank you, you solved my issues! Commented Jun 22, 2020 at 22:18
  • No problem, just one more thing, $169.99 will not show because $ expands to variables in powershell so use $$169.99, "`$169.99" or '$169.99' Commented Jun 22, 2020 at 22:20

1 Answer 1

1

XMLs are case-sensitive so an XML like this:

<test></Test>

Will also not work having the error

Cannot convert value "<test></Test>" to type "System.Xml.XmlDocument". Error: "The 'test'
start tag on line 1 position 2 does not match the end tag of 'Test'. Line 1, position 9."
At line:1 char:1
+ [xml]"<test></Test>"
+ ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvalidCastToXmlDocument

In your XML, the starting and ending tags are different:

[Xml]@"
<Mylist>
<!--l is not capitalized-->
  <Item Number="1">
    <Name>"AMD Ryzen 5 3600x"</Name>
    <Type>"CPU"</Type>
    <Price>"$169.99"</Price>
    <Where>"Amazon"</Where>
    <Other>
      <ThreadCount>"12"</ThreadCount>
      <Cores>"6"</Cores>
    </Other>
  </Item>
</MyList>
<!--l is capitalized-->
"@

Which means that <Mylist> and </MyList> are two different nodes.

This is what is causing your error so try

[Xml]$MyXmlVariable = @"
<MyList>
  <Item Number="1">
    <Name>"AMD Ryzen 5 3600x"</Name>
    <Type>"CPU"</Type>
    <Price>"$169.99"</Price>
    <Where>"Amazon"</Where>
    <Other>
      <ThreadCount>"12"</ThreadCount>
      <Cores>"6"</Cores>
    </Other>
  </Item>
</MyList>
"@
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.