【问题标题】:how to remove default namespaces and add custom namespace in root tag of xml using C#?如何使用 C# 在 xml 的根标记中删除默认命名空间并添加自定义命名空间?
【发布时间】:2023-03-10 13:22:01
【问题描述】:

从 C# 类创建 xml 时,我在根标记 (Order) 中获得了一些默认命名空间 (xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"),如下所示。但是,我想删除那些默认命名空间,并且我需要根标记中的以下命名空间(订单xmlns="http://example.com/xml/1.0")。 如何删除这些默认命名空间并在 c# 代码中替换。提前致谢。

<?xml version="1.0"?>
<Order xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <Number Type="mobile">9999999999</Number>
   <TrackStartDateTime>2015-05-30 11:00 ET</TrackStartDateTime>
   <Notifications>
      <Notification>
         <PartnerMPID>99999999</PartnerMPID>
         <IDNumber>L563645</IDNumber>
         <TrackDurationInHours>120</TrackDurationInHours>
         <TrackIntervalInMinutes>240</TrackIntervalInMinutes>
      </Notification>
   </Notifications>
   <Carrier>
      <Dispatcher>
         <DispatcherName>?</DispatcherName>
         <DispatcherPhone>0</DispatcherPhone>
         <DispatcherEmail>?</DispatcherEmail>
      </Dispatcher>
   </Carrier>
</Order>

我使用过以下 C# 类。

    [XmlRoot("Order")]
    public class Order
    {
        [XmlElement("Number")]
        public Number Number;

        [XmlElement("TrackStartDateTime")]
        public string TrackStartDateTime;//TODO - need to check         

        [XmlElement("Notifications")]
        public Notifications Notifications;//TODO - notification tag should come inside Notifications tag

        [XmlElement("Carrier")]
        public Carrier Carrier;           

        public Order() {
            Number = new Number();
            Notifications = new Notifications();
            Carrier = new Carrier();
            TripSheet = new TripSheet();
        }
    }

    public class Number
    {
        [XmlAttribute("Type")]
        public string Type;

        [XmlText]
        public Int64 Value;
    }

    public class Notifications {

        [XmlElement("Notification")]
        public List<Notification> Notification;
        public Notifications() {
            Notification = new List<Notification>();
        }

    }
    public class Notification
    {
        [XmlElement("PartnerMPID")]
        public string PartnerMPID { get; set; }

        [XmlElement("IDNumber")]
        public string IDNumber { get; set; }

        [XmlElement("TrackDurationInHours")]
        public int TrackDurationInHours { get; set; }

        [XmlElement("TrackIntervalInMinutes")]
        public int TrackIntervalInMinutes { get; set; }

    }
    public class Carrier
    {
        [XmlElement("Name")]
        public string Name;

        [XmlElement("Dispatcher")]
        public Dispatcher Dispatcher;
        public Carrier() {
            Dispatcher = new Dispatcher();
        }

    }
    public class Dispatcher
    {
        [XmlElement("DispatcherName")]
        public string DispatcherName;

        [XmlElement("DispatcherPhone")]
        public Int64 DispatcherPhone;

        [XmlElement("DispatcherEmail")]
        public string DispatcherEmail;//conform format for email         

    }

我采用了 Order Class 的新实例,出于测试目的,我为每个字段设置了硬编码值,并使用以下代码从 C# 类创建 xml。

    public string CreateXML(Order order)
    {
        XmlDocument xmlDoc = new XmlDocument(); 

        XmlSerializer xmlSerializer = new XmlSerializer(typeof(Order));  

        // Creates a stream whose backing store is memory. 
        using (MemoryStream xmlStream = new MemoryStream())
        {                
            xmlSerializer.Serialize(xmlStream, order);//,ns  
            xmlStream.Position = 0;                
            //Loads the XML document from the specified string.
            xmlDoc.Load(xmlStream);
            return xmlDoc.InnerXml;
        }
    }

我不确定它是从 C# 类创建 xml 的正确方法。请指导我从 c# 类中获取以下 xml 输出。

<?xml version="1.0"?>
<Order xmlns="http://example.com/xml/1.0" >
   <Number Type="mobile">9999999999</Number>
   <TrackStartDateTime>2015-05-30 11:00 ET</TrackStartDateTime>
   <Notifications>
      <Notification>
         <PartnerMPID>99999999</PartnerMPID>
         <IDNumber>L563645</IDNumber>
         <TrackDurationInHours>120</TrackDurationInHours>
         <TrackIntervalInMinutes>240</TrackIntervalInMinutes>
      </Notification>
   </Notifications>
   <Carrier>
      <Dispatcher>
         <DispatcherName>?</DispatcherName>
         <DispatcherPhone>0</DispatcherPhone>
         <DispatcherEmail>?</DispatcherEmail>
      </Dispatcher>
   </Carrier>
</Order>

【问题讨论】:

  • 你用什么代码来生成这个xml?
  • 这听起来像是XY problem。如果我猜的话,您正在使用 XmlSerializer 来生成 XML。可以在序列化期间控制默认命名空间和Order 的命名空间的包含。如果是这种情况,请包含您的序列化代码。

标签: c# xml asp.net-mvc namespaces


【解决方案1】:

对于这种情况,只需将空命名空间添加到类定义中使用的 XMLRoot 装饰就足够了。

[XmlRoot("Order", Namespace = null)]
public class Order
{
    [XmlElement("Number")]
    public Number Number;

    [XmlElement("TrackStartDateTime")]
    public string TrackStartDateTime;         

    [XmlElement("Notifications")]
    public Notifications Notifications;

    [XmlElement("Carrier")]
    public Carrier Carrier;       

序列化程序应该完成其余的工作。

【讨论】:

    【解决方案2】:

    这是一种方法... 只需创建一个新的XDocument 并在其上设置您想要的命名空间并将原始的 xml 后代移植到其中,如下所示:

            var xml = "<?xml version=\"1.0\"?><Order xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><Number Type=\"mobile\">9999999999</Number></Order>";
    
            var xdoc = XDocument.Parse(xml);            
    
            var ns = XNamespace.Get("http://example.com/xml/1.0");
            var xdoc2 = new XDocument(new XDeclaration("1.0", null, null),
                                     new XElement(ns + "Order", xdoc.Root.Descendants()));
    

    工作示例请参见此处:https://dotnetfiddle.net/JYCL95

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-23
      • 2011-07-18
      • 1970-01-01
      • 2016-12-18
      • 1970-01-01
      • 2018-08-25
      • 1970-01-01
      相关资源
      最近更新 更多