在XMLSerialzation添加缺少的属性

时间:2013-03-09 03:50:30

标签: c# xml-serialization xmlserializer

如何让IPDR节点看起来像这样:

<ns2:IPDR xmlns:ns0="http://www.ipdr.org/namespaces/PWA" xsi:type="ns0:PublicWLANAccessUsageEntry">

而不是:

<ns2:IPDR xsi:type="ns0:PublicWLANAccessUsageEntry">

在不加载xml文件的情况下,它会生成xmlDoc并添加它。我希望在调用XmlSerializer.Serialize时执行此操作。

预期:

    <?xml version="1.0" encoding="utf-8"?>
<ns2:IPDRDoc xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns0="http://www.ipdr.org/namespaces/PWA" xmlns:ns1="http://www.w3.org/2001/XMLSchema" docId="xxx" version="3.5.1" creationTime="2013-03-09T01:17:27Z" IPDRRecorderInfo="xxx" xmlns:ns2="http://www.ipdr.org/namespaces/ipdr">
  <ns2:IPDR xmlns:ns0="http://www.ipdr.org/namespaces/PWA" xsi:type="ns0:PublicWLANAccessUsageEntry">
    <ns2:IPDRCreationTime></ns2:IPDRCreationTime>
    <ns2:seqNum></ns2:seqNum>
    <ns0:userName></ns0:userName>
    <ns0:subscriberID></ns0:subscriberID>
    <ns0:sessionID></ns0:sessionID>
    <ns0:callingStationID></ns0:callingStationID>
    <ns0:locationPort></ns0:locationPort>
    <ns0:calledStationID></ns0:calledStationID>
    <ns0:NASIPaddress></ns0:NASIPaddress>
    <ns0:NASID></ns0:NASID>
    <ns0:locationName></ns0:locationName>
    <ns0:locationID></ns0:locationID>
    <ns0:locationCountryCode></ns0:locationCountryCode>
    <ns0:locationStateProvince></ns0:locationStateProvince>
    <ns0:locationCity></ns0:locationCity>
    <ns0:NASPortType></ns0:NASPortType>
    <ns0:networkConnectionType></ns0:networkConnectionType>
    <ns0:sessionDuration></ns0:sessionDuration>
    <ns0:unitOfMeasure></ns0:unitOfMeasure>
    <ns0:chargeableUnit></ns0:chargeableUnit>
    <ns0:chargeableQuantityCumulative></ns0:chargeableQuantityCumulative>
    <ns0:inputOctetsCumulative></ns0:inputOctetsCumulative>
    <ns0:outputOctetsCumulative></ns0:outputOctetsCumulative>
    <ns0:GMTSessionStartDateTime>
      <ns0:sessionStartDateTime></ns0:sessionStartDateTime>
      <ns0:timeZoneOffset></ns0:timeZoneOffset>
    </ns0:GMTSessionStartDateTime>
    <ns0:GMTSessionEndDateTime>
      <ns0:sessionEndDateTime></ns0:sessionEndDateTime>
      <ns0:timeZoneOffset></ns0:timeZoneOffset>
    </ns0:GMTSessionEndDateTime>
    <ns0:sessionTerminationCause></ns0:sessionTerminationCause>
    <ns0:acctStatusType></ns0:acctStatusType>
  </ns2:IPDR>
</ns2:IPDRDoc>

实际值:

    <?xml version="1.0" encoding="utf-8"?>
<ns2:IPDRDoc xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns0="http://www.ipdr.org/namespaces/PWA" xmlns:ns1="http://www.w3.org/2001/XMLSchema" docId="xxx" version="3.5.1" creationTime="2013-03-09T02:53:36Z" IPDRRecorderInfo="xxx" xmlns:ns2="http://www.ipdr.org/namespaces/ipdr">
  <ns2:IPDR xsi:type="ns0:PublicWLANAccessUsageEntry">
    <ns2:IPDRCreationTime></ns2:IPDRCreationTime>
    <ns2:seqNum></ns2:seqNum>
    <ns0:userName></ns0:userName>
    <ns0:subscriberID></ns0:subscriberID>
    <ns0:sessionID></ns0:sessionID>
    <ns0:callingStationID></ns0:callingStationID>
    <ns0:locationPort></ns0:locationPort>
    <ns0:calledStationID></ns0:calledStationID>
    <ns0:NASIPaddress></ns0:NASIPaddress>
    <ns0:NASID></ns0:NASID>
    <ns0:locationName></ns0:locationName>
    <ns0:locationID></ns0:locationID>
    <ns0:locationCountryCode></ns0:locationCountryCode>
    <ns0:locationStateProvince></ns0:locationStateProvince>
    <ns0:locationCity></ns0:locationCity>
    <ns0:NASPortType></ns0:NASPortType>
    <ns0:networkConnectionType></ns0:networkConnectionType>
    <ns0:sessionDuration></ns0:sessionDuration>
    <ns0:unitOfMeasure></ns0:unitOfMeasure>
    <ns0:chargeableUnit></ns0:chargeableUnit>
    <ns0:chargeableQuantityCumulative></ns0:chargeableQuantityCumulative>
    <ns0:inputOctetsCumulative></ns0:inputOctetsCumulative>
    <ns0:outputOctetsCumulative></ns0:outputOctetsCumulative>
    <ns0:GMTSessionStartDateTime>
      <ns0:sessionStartDateTime></ns0:sessionStartDateTime>
      <ns0:timeZoneOffset></ns0:timeZoneOffset>
    </ns0:GMTSessionStartDateTime>
    <ns0:GMTSessionEndDateTime>
      <ns0:sessionEndDateTime></ns0:sessionEndDateTime>
      <ns0:timeZoneOffset></ns0:timeZoneOffset>
    </ns0:GMTSessionEndDateTime>
    <ns0:sessionTerminationCause></ns0:sessionTerminationCause>
    <ns0:acctStatusType></ns0:acctStatusType>
  </ns2:IPDR>

My Code Snippets:

[System.SerializableAttribute]
[System.Diagnostics.DebuggerStepThroughAttribute]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = false, Namespace = "http://www.ipdr.org/namespaces/ipdr", IncludeInSchema = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://www.ipdr.org/namespaces/ipdr", IsNullable = false)]
public partial class IPDRDoc
{
}

[System.SerializableAttribute]
[System.Diagnostics.DebuggerStepThroughAttribute]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.ipdr.org/namespaces/PWA", IncludeInSchema = true, AnonymousType = false)]
public partial class PublicWLANAccessUsageEntry : IPDRType // this is the IPDR Node
{
}

XmlSerializer ser = new XmlSerializer(typeof(IPDRDoc));
FileStream stream = new FileStream(filePath, FileMode.Create);
StreamWriter writer = new StreamWriter(stream, new UTF8Encoding());
XmlSerializerNamespaces xmlns = new XmlSerializerNamespaces();
xmlns.Add("ns2", "http://www.ipdr.org/namespaces/ipdr");
xmlns.Add("ns0", "http://www.ipdr.org/namespaces/PWA");
xmlns.Add("ns1", "http://www.w3.org/2001/XMLSchema");
xmlns.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
ser.Serialize(writer, Ipdr, xmlns);

0 个答案:

没有答案