.net 从根节点下删除所有名称空间
我正在使用System.Xml.Serialization将类序列化为xdocument.net 从根节点下删除所有名称空间,.net,xml,vb.net,serialization,namespaces,.net,Xml,Vb.net,Serialization,Namespaces,我正在使用System.Xml.Serialization将类序列化为xdocument <tns:RatingRequest xmlns:tns="http://somewebsite/services/rating" xmlns:tns1="http://somewebsite/services/rating" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://some
<tns:RatingRequest xmlns:tns="http://somewebsite/services/rating"
xmlns:tns1="http://somewebsite/services/rating"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://somewebsite/services/rating.xsd ">
<tns:Configuration>
<tns:Client>
<tns:TradingPartnerNum>101010</tns:TradingPartnerNum>
</tns:Client>
</tns:Configuration>
<tns:PickupDate>2017-12-12T00:00:00</tns:PickupDate>
<tns:LatestDeliveryDate>0001-01-01T00:00:00</tns:LatestDeliveryDate>
<tns:Stops>
<tns:Index>1</tns:Index>
</tns:Stops>
</tns:RatingRequest>
101010
2017-12-12T00:00:00
0001-01-01T00:00:00
1.
我需要的只是第一个具有tns:namespace-like的节点
<tns:RatingRequest xmlns:tns="http://somewebsite/services/rating"
xmlns:tns1="http://somewebsite/services/rating"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://somewebsite/services/rating.xsd ">
<Configuration>
<TradingPartner>
<TradingPartnerNum>101010</TradingPartnerNum>
</TradingPartner>
</Configuration>
<PickupDate>2017-10-27T00:00:00-05:00</PickupDate>
<DeliveryDate>-05:00</DeliveryDate>
<Stops>
<Stop>
<Index>1</Index>
</stop>
</stops>
</tns:RatingRequest>
101010
2017-10-27T00:00:00-05:00
-05:00
1.
有一种干净的方法可以做到这一点吗?这里的技巧是,在您想要的xml中,子元素的名称空间是空的名称空间。您的根元素位于
中http://somewebsite/services/rating“
,默认情况下继承命名空间;因此:您需要在用于子元素的任何xml序列化程序属性上包含Namespace=”“
。例如,如果您有:
[XmlElement("PickupDate")]
public DateTime SomeDate {get;set;}
那么它可能会变成:
[XmlElement("PickupDate", Namespace = "")]
public DateTime SomeDate {get;set;}
您需要对其他元素重复该操作。这两个xml片段描述不同的数据-子元素的命名空间不同。如果你想要最下面的一个:告诉它使用根名称空间,这是完美和简单的。非常感谢。