XML属性没有得到命名空间前缀 [英] XML attribute not getting namespace prefix

查看:191
本文介绍了XML属性没有得到命名空间前缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要生成序列化过程中下面的XML:
(片段)

I need to generate the following XML during serialization: (fragment)

<IncidentEvent a:EventTypeText="Beginning" xmlns:a="http://foo">
  <EventDate>2013-12-18</EventDate>
  <EventTime>00:15:28</EventTime>
</IncidentEvent>



有关类别如下:

The class in question looks like this:

public class IncidentEvent
{
    public string EventDate { get; set; }
    public string EventTime { get; set; }

    [XmlAttribute("EventTypeText", Namespace = "http://foo")]
    public string EventTypeText { get; set; }

}



看来,串行器注意到,命名空间在XMLNS已经声明:在根和无视我的属性。我也试过以下内容:

It appears that the serializer is noticing that the namespace is already declared in an xmlns: at the root and is ignoring my attribute. I also tried the following:

[XmlRoot(Namespace = "http://foo")]
public class IncidentEvent
{
    public string EventDate { get; set; }
    public string EventTime { get; set; }

    private XmlSerializerNamespaces _Xmlns;

    [XmlNamespaceDeclarations]
    public XmlSerializerNamespaces Xmlns
    {
        get
        {
            if (_Xmlns == null)
            {
                _Xmlns = new XmlSerializerNamespaces();
                _Xmlns.Add("ett", "http://foo");
            }

            return _Xmlns;
        }

        set
        {
            _Xmlns = value;
        }
    }


    [XmlAttribute("EventTypeText", Namespace = "http://foo")]
    public string EventTypeText { get; set; }

}

这将导致以下XML:

  <ett:IncidentEvent EventTypeText="Beginning" xmlns:ett="http://foo">
    <ett:EventDate>2013-12-18</ett:EventDate>
    <ett:EventTime>00:15:28</ett:EventTime>
  </ett:IncidentEvent>



这是不是我想要的。不应前缀元素,属性应。什么是需要得到串行明白我想要什么?

Which is not what I want. The element shouldn't be prefixed, the attribute should be. What is needed to get the serializer to understand what I want?

推荐答案

我做了一些研究,可以以下的答案可以帮助

I did some research may be following answer helps

有关属性有命名空间前缀必须指定比你指定什么其他的不同的命名空间标记的http://富 。下面的代码希望能解决您的问题。在代码中,我删除元素的名称空间,只为属性添加的。

For Attributes to have namespace prefix you have to specify a different namespace tag other than what you have specified http://foo. Following code hopefully will solve your issue. In the code i have remove the namespace for elements and added only for the attribute.

public class IncidentEvent
{
    public string EventDate { get; set; }
    public string EventTime { get; set; }

    [XmlAttribute("EventTypeText", Namespace = "http://foo")]
    public string EventTypeText { get; set; }

}

class Program
{
    static void Main(string[] args)
    {
        IncidentEvent xmlObj = new IncidentEvent()
        {
            EventDate = "2012.12.01",
            EventTime = "1:00:00",
            EventTypeText = "Beginining"
        };
        XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
        ns.Add("ett", "http://foo");
        XmlSerializer serializer = new XmlSerializer(typeof(IncidentEvent));
        serializer.Serialize(Console.OpenStandardOutput(), xmlObj, ns);
        Console.WriteLine();
    }
}

http://www.w3.org/TR/2009/REC-xml-names-20091208/#defaulting

这篇关于XML属性没有得到命名空间前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆