我们从 SoapUI 项目 Xsd 生成了这个类:
[System.CodeDom.Compiler.GeneratedCodeAttribute("MSBuild", "4.0.30319.18408")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://eviware.com/soapui/config")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://eviware.com/soapui/config", IsNullable=true)]
public partial class RestRequestStep : object, System.ComponentModel.INotifyPropertyChanged
{
public RestRequest restRequest;
[System.Xml.Serialization.XmlAttributeAttribute()]
public string service;
[System.Xml.Serialization.XmlAttributeAttribute()]
public string resourcePath;
[System.Xml.Serialization.XmlAttributeAttribute()]
public string methodName;
}
并且项目文档包含一个名为 config 的 xsd:anyType 元素,其中包含以下 xml
<con:config service="api" resourcePath="xxx" methodName="GET" xsi:type="con:RestRequestStep" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<con:restRequest name="Request 1" mediaType="application/json">
<con:settings>
<con:setting id="com.eviware.soapui.impl.wsdl.WsdlRequest@request-headers"><xml-fragment/></con:setting>
</con:settings>
<con:endpoint>${#Project#CurrentEndpoint}</con:endpoint>
<con:request/>
<con:originalUri>http://localhost/</con:originalUri>
<con:assertion type="Valid HTTP Status Codes" name="Valid HTTP Status Codes">
<con:configuration>
<codes>200</codes>
</con:configuration>
</con:assertion>
<con:assertion type="Schema Compliance" name="Schema Compliance">
<con:configuration>
<definition/>
</con:configuration>
</con:assertion>
<con:credentials>
<con:authType>No Authorization</con:authType>
</con:credentials>
<con:jmsConfig JMSDeliveryMode="PERSISTENT"/>
<con:jmsPropertyConfig/>
<con:parameters>
<entry key="connectionName" value="${#Project#ConnectionName}" xmlns="http://eviware.com/soapui/config"/>
</con:parameters>
</con:restRequest>
</con:config>
在该文档的包装器中,该config
属性的类型为object
在运行时, 的内容config
是包含元素XmlNode[]
的所有子节点的。config
我需要把它XmlNode[]
变成它应该的类型,RestRequestStep
到目前为止,我有这个:
public static T FromXml<T>(this IEnumerable<XmlNode> input)
{
T output;
var type = typeof(T);
var serializer = CreateSerializer(type);
var doc = new XmlDocument();
var rootAttribute = (XmlRootAttribute)type.GetCustomAttributes(true).Where(a => a is XmlRootAttribute).SingleOrDefault();
string ns = null;
if (rootAttribute != null)
{
ns = rootAttribute.Namespace;
}
doc.AppendChild(doc.CreateElement(type.Name, ns));
foreach (var node in input)
{
var inode = doc.ImportNode(node, true);
if (inode is XmlAttribute)
{
doc.DocumentElement.Attributes.Append((XmlAttribute)inode);
}
else
{
doc.DocumentElement.AppendChild(inode);
}
}
output = (T)serializer.Deserialize(new StringReader(doc.OuterXml));
return output;
}
但这失败了output = (T)serializer.Deserialize(new StringReader(doc.OuterXml));
,出现以下异常:
System.InvalidOperationException was unhandled
Message=There is an error in XML document (1, 2).
Source=System.Xml
InnerException: System.InvalidOperationException
Message=Namespace prefix 'con' is not defined.
Source=System.Xml
的内容OuterXml
最终为:
<RestRequestStep service="api" resourcePath="xxx" methodName="GET" xsi:type="con:RestRequestStep" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://eviware.com/soapui/config">
<con:restRequest name="Request 1" mediaType="application/json" xmlns:con="http://eviware.com/soapui/config">
<con:settings>
<con:setting id="com.eviware.soapui.impl.wsdl.WsdlRequest@request-headers"><xml-fragment/></con:setting>
</con:settings>
<con:endpoint>${#Project#CurrentEndpoint}</con:endpoint>
<con:request/>
<con:originalUri>http://localhost/</con:originalUri>
<con:assertion type="Valid HTTP Status Codes" name="Valid HTTP Status Codes">
<con:configuration>
<codes xmlns="">200</codes>
</con:configuration>
</con:assertion>
<con:assertion type="Schema Compliance" name="Schema Compliance">
<con:configuration>
<definition xmlns=""/>
</con:configuration>
</con:assertion>
<con:credentials>
<con:authType>No Authorization</con:authType>
</con:credentials>
<con:jmsConfig JMSDeliveryMode="PERSISTENT"/>
<con:jmsPropertyConfig/>
<con:parameters>
<entry key="connectionName" value="${#Project#ConnectionName}" xmlns="http://eviware.com/soapui/config"/>
</con:parameters>
</con:restRequest>
</RestRequestStep>
的内容不应该config
是 aRestRequestStep
而不是 aXmlNode[]
吗?
如何将一个反序列XmlNode[]
化为 T 类型?