'Cast string representation to ManagementObject in System.Management

When executing functions in WMI using C# and System.Management, some functions, such as CreateSnapshot, take a string representation as an [in] parameter. This is quite easy to do, by constructing the ManagementObject and then calling .GetText() on it.

I'm now trying to use GetVirtualHardDiskSettingData which has the same, but as an [out] paramter.

After execution, the "SettingData" parameter contains an embedded string representation of a Msvm_VirtualHardDiskSettingData instance. When cast to a string, it looks like XML (technically, I guess this would be DTD 2.0):

<INSTANCE CLASSNAME="Msvm_VirtualHardDiskSettingData"><PROPERTY NAME="BlockSize" TYPE="uint32"><VALUE>2097152</VALUE></PROPERTY><PROPERTY NAME="Caption" TYPE="string"><VALUE>Virtual Hard Disk Setting Data</VALUE></PROPERTY><PROPERTY NAME="DataAlignment" PROPAGATED="true" TYPE="uint64"></PROPERTY><PROPERTY NAME="Description" TYPE="string"><VALUE>Setting Data for a Virtual Hard Disk.</VALUE></PROPERTY><PROPERTY NAME="ElementName" TYPE="string"><VALUE>HSX1_35FA3B00-4CDC-4C75-A6BB-0A520F798FC1.avhdx</VALUE></PROPERTY><PROPERTY NAME="Format" TYPE="uint16"><VALUE>3</VALUE></PROPERTY><PROPERTY NAME="InstanceID" TYPE="string"><VALUE>FB657867-697F-4F40-B360-46C897E41379</VALUE></PROPERTY><PROPERTY NAME="IsPmemCompatible" TYPE="boolean"><VALUE>FALSE</VALUE></PROPERTY>
...
<PROPERTY NAME="Type" TYPE="uint16"><VALUE>4</VALUE></PROPERTY><PROPERTY NAME="VirtualDiskId" TYPE="string"><VALUE>FB657867-697F-4F40-B360-46C897E41379</VALUE></PROPERTY></INSTANCE>

I'd like to cast this string representation back to a ManagementObject (or something similar). Simply casting it does not work. Neither does there seem to be any sort of constructor or method available to offer the opposite of the GetText() method.

After Googling, I found the SpawnInstance method, which seems to do what I'm looking for, but for C++.

There is the ManagementClass.CreateInstance method, which seems similar, but does not take any parameters.

Is there anything baked in to System.Management to do this? Or is it "safe" to just parse this as raw XML? I know "representations" can be either in DTD or Mof format, so I don't think I can rely on this being XML every time.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source