1

如何让 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>

我的代码片段:

[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);
4

0 回答 0