Reputation: 13
I am using System.Xml.Serialization to serialize a class into an 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://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>
What I need is only the first node having the 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>
Is there a clean way of doing this?
Upvotes: 1
Views: 255
Reputation: 1063884
The trick here is that in the xml you want, the namespace of the child elements is the empty namespace. Your root element is in the "http://somewebsite/services/rating"
, and by default the namespace is inherited; so: you need to include Namespace = ""
on whatever xml serializer attributes you are using for the child elements. For example, if you have:
[XmlElement("PickupDate")]
public DateTime SomeDate {get;set;}
then it might become:
[XmlElement("PickupDate", Namespace = "")]
public DateTime SomeDate {get;set;}
You will need to repeat that for the other elements.
Upvotes: 3