我正在尝试将xsd文件转换为.net类。 我已经搜索了很多这个主题,并发现xsd.exe是实现它的方法之一,但我仍有两个问题
我不想从命令提示符手动生成类,但希望在运行时完成所有这些操作。为此,我尝试使用System.Diagnostics.Process在运行时运行xsd.exe但无法成功,并且在进程启动时也会获得一个闪烁的命令提示符窗口。
我甚至没有成功获取从命令提示符生成的类。它给了我错误“架构D:\ Response.xsd无法验证。”
所以基本上我正在尝试实现一些将使用我的xsd字符串并将其反序列化为运行时类的一个类型的东西,就像我们使用XmlSerializer类对Xml字符串一样。
我想提一下,我认为我的xsd字符串不是一般的xsd类型,而是一种自定义,其中一个响应的例子就是这个
<?xml version="1.0" encoding="iso-8859-1"?>
<serv:message xmlns:serv="http://www.webex.com/schemas/2002/06/service"
xmlns:com="http://www.webex.com/schemas/2002/06/common"
xmlns:use="http://www.webex.com/schemas/2002/06/service/user">
<serv:header>
<serv:response>
<serv:result>SUCCESS</serv:result>
<serv:gsbStatus>BACKUP</serv:gsbStatus>
</serv:response>
</serv:header>
<serv:body>
<serv:bodyContent xsi:type="use:getLoginTicketResponse"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<use:ticket>5e9733eb9efeb02d80aa0551ef7e9ccd</use:ticket>
<use:apiVersion>WebEx XML API V3.9.0</use:apiVersion>
</serv:bodyContent>
</serv:body>
</serv:message>
修改 - 我成功地为我的xml生成了类文件,现在它是
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.webex.com/schemas/2002/06/service/user")]
public partial class getLoginTicketResponse : bodyContentType
{
private string ticketField;
private string apiVersionField;
/// <remarks/>
public string ticket
{
get
{
return this.ticketField;
}
set
{
this.ticketField = value;
}
}
/// <remarks/>
public string apiVersion
{
get
{
return this.apiVersionField;
}
set
{
this.apiVersionField = value;
}
}
}
这是bodyContentType
/// <remarks/>
[System.Xml.Serialization.XmlIncludeAttribute(typeof(getLoginTicketResponse))]
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.webex.com/schemas/2002/06/service")]
[System.Xml.Serialization.XmlRootAttribute("getLoginTicket", Namespace = "http://www.webex.com/schemas/2002/06/service/user", IsNullable = false)]
public partial class bodyContentType
{
}
现在,尝试像这样序列化
var nameSpaceManager = new XmlNamespaceManager(responseXML.NameTable);
nameSpaceManager.AddNamespace("serv", "http://www.webex.com/schemas/2002/06/service");
nameSpaceManager.AddNamespace("com", "http://www.webex.com/schemas/2002/06/common");
nameSpaceManager.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
nameSpaceManager.AddNamespace("meet", "http://www.webex.com/schemas/2002/06/service/meeting");
nameSpaceManager.AddNamespace("att", "http://www.webex.com/schemas/2002/06/service/attendee");
nameSpaceManager.AddNamespace("use", "http://www.webex.com/schemas/2002/06/service/user");
XmlNode statusNode = responseXML.SelectSingleNode("/serv:message/serv:body", nameSpaceManager);
TextReader reader = new StringReader(s);
XmlSerializer serializer = new XmlSerializer(typeof(getLoginTicketResponse));
var obj = serializer.Deserialize(reader);
但继续得到错误 “XML文档中存在错误(1,2)。” 我尝试在xml字符串中更改许多内容,但无法成功
任何帮助都会很明显!
答案 0 :(得分:0)
使用XDocument有一个非常体面和简单的方法来反序列化我的xml! :):)
以下是我现在正在做的事情
var xDoc = XDocument.Parse(xmlString);
XNamespace userNS = "http://www.webex.com/schemas/2002/06/service/user";
XNamespace servNS = "http://www.webex.com/schemas/2002/06/service";
var re = from rssItem in xDoc.Descendants(servNS + "bodyContent")
let elementTicket = rssItem.Element(userNS + "ticket")
let elementApiVersion = rssItem.Element(userNS + "apiVersion")
where (elementTicket != null && elementApiVersion != null)
select new getLoginTicketResponse()
{
ticket = elementTicket.Value,
apiVersion = elementApiVersion.Value
};
getLoginTicketResponse loginTicketResponse = re.First();
我直接从xml反序列化我的类对象,不需要XSD !
让我再次向您展示我的xml
<?xml version="1.0" encoding="iso-8859-1"?>
<serv:message xmlns:serv="http://www.webex.com/schemas/2002/06/service"
xmlns:com="http://www.webex.com/schemas/2002/06/common"
xmlns:use="http://www.webex.com/schemas/2002/06/service/user">
<serv:header>
<serv:response>
<serv:result>SUCCESS</serv:result>
<serv:gsbStatus>BACKUP</serv:gsbStatus>
</serv:response>
</serv:header>
<serv:body>
<serv:bodyContent xsi:type="use:getLoginTicketResponse"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<use:ticket>5e9733eb9efeb02d80aa0551ef7e9ccd</use:ticket>
<use:apiVersion>WebEx XML API V3.9.0</use:apiVersion>
</serv:bodyContent>
</serv:body>
</serv:message>