在xml中添加前缀和名称空间,未在正确的位置XML序列化中设置
问题描述:
我正在使用一个应用程序,因为我需要使用SOAP方法发布xml,下面提供了xml示例。我用xml序列化,但是当我发布xml时,xml的格式不正确,因为它是在xml中提供的。我在做xml序列化的错误。所以前缀不会显示在我的xml中的正确位置。在xml中添加前缀和名称空间,未在正确的位置XML序列化中设置
示例XML
<SOAP:Envelope xmlns:SOAP='http://schemas.xmlsoap.org/soap/envelope/' >
<SOAP:Body UserGUID = '{3333333-F333-3333-6666-00CDEFGH34555}' >
<m:SaveOrder xmlns:m = 'http://www.e-courier.com/schemas/' >
<Order UserID = '1' Notes = ' Testing.' CustomerID = '1' >
<Customer />
<Stops >
<Stop Sequence = '1' StopType = 'P' >
<OrderStopPieces >
<OrderStopPiece Sequence = '1' PieceAction = 'P' />
</OrderStopPieces >
</Stop >
</Stops >
</Order >
</m:SaveOrder >
</SOAP:Body >
</SOAP:Envelope >
我的代码转换XML
**不知道在编辑模式下我的XML被显示,但视图模式不显示**
我的类XML serilization的
[XmlRoot(ElementName = "Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public class Envelope
{
[XmlElement(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public Body Body { get; set; }
}
[XmlRoot(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public class Body
{
[XmlElement(ElementName = "SaveOrder", Namespace = "http://www.e-courier.com/schemas/public/")]
public SaveOrder SaveOrder { get; set; }
[XmlAttribute(AttributeName = "UserGUID")]
public string UserGUID { get; set; }
}
public class SaveOrder
{
[XmlElement(ElementName = "Order")]
public Order Order { get; set; }
// [XmlAttribute(AttributeName = "m", Namespace = "http://www.w3.org/2000/xmlns/")]
// public string M { get; set; }
}
public class Order
{
[XmlAttribute(AttributeName = "UserID")]
public string UserID { get; set; }
[XmlAttribute(AttributeName = "Notes")]
public string Notes { get; set; }
[XmlAttribute(AttributeName = "CustomerID")]
public string CustomerID { get; set; }
}
我对设置的命名空间的代码和前缀
var ns = new XmlSerializerNamespaces();
ns.Add("SOAP", "http://schemas.xmlsoap.org/soap/envelope/");
ns.Add("m", "http://www.e-courier.com/software/schema/public/");
答
您有前缀m:
相关的错误命名空间:
ns.Add("m", "http://www.e-courier.com/software/schema/public/");
这不会出现在示例XML。
这应该是:
ns.Add("m", "http://www.e-courier.com/schemas/");
注意,不管它是用一个前缀呈现与否,是无关紧要的。
此:
<m:SaveOrder xmlns:m = 'http://www.e-courier.com/schemas/' >
是一样的:
<SaveOrder xmlns='http://www.e-courier.com/schemas/' >
@GregHNH我改变了代码,按您的建议,但是它产生不添加m:在SaveOrder中和xmlns:m在xml中 –