代码之家  ›  专栏  ›  技术社区  ›  Barry Kelly

如何为子节点创建具有默认命名空间的XElement,而不在所有子节点中使用XNamespace

  •  9
  • Barry Kelly  · 技术社区  · 17 年前

    我正在尝试使用 System.Xml.Linq 创建XHTML文档。因此,我的树中的绝大多数节点都应该使用这个名称空间:

    http://www.w3.org/1999/xhtml
    

    我可以创造 XElement 使用 XNamespace ,就像这样:

    XNamespace xhtml = "http://www.w3.org/1999/xhtml";
    // ...
    new XElement(xhtml + "html", // ...
    

    但是,我不想做一个 XNamespace 在创建HTML节点的所有代码中都可用,并且必须为每个节点添加前缀 XElement (以及 XAttribute )我创建相应的名称。

    XML文本格式本身考虑了这一要求,并允许使用保留的 xmlns 属性。我想用 系统。Xml。方法 .

    这可能吗?

    3 回复  |  直到 15 年前
        1
  •  5
  •   Barry Kelly    17 年前

    我决定使用一个名为 XHtml ,看起来像这样:

    public static class XHtml
    {
        static XHtml()
        {
            Namespace = "http://www.w3.org/1999/xhtml";
        }
    
        public static XNamespace Namespace { get; private set; }
    
        public static XElement Element(string name)
        {
            return new XElement(Namespace + name);
        }
    
        public static XElement Element(string name, params object[] content)
        {
            return new XElement(Namespace + name, content);
        }
    
        public static XElement Element(string name, object content)
        {
            return new XElement(Namespace + name, content);
        }
    
        public static XAttribute Attribute(string name, object value)
        {
            return new XAttribute(/* Namespace + */ name, value);
        }
    
        public static XText Text(string text)
        {
            return new XText(text);
        }
    
        public static XElement A(string url, params object[] content)
        {
            XElement result = Element("a", content);
            result.Add(Attribute("href", url));
            return result;
        }
    }
    

    这似乎是最干净的做事方式,尤其是当我可以添加便利程序时,比如 XHtml.A 方法(这里没有显示我的所有类)。

        2
  •  3
  •   Julian Lettner    16 年前

    我采取了递归重写的方式。你真的不必“重建”这棵树。你可以换掉节点名称( XName ).

        private static void ApplyNamespace(XElement parent, XNamespace nameSpace)
        {
            if(DetermineIfNameSpaceShouldBeApplied(parent, nameSpace))
            {
                parent.Name = nameSpace + parent.Name.LocalName;
            }
            foreach (XElement child in parent.Elements())
            {
                ApplyNamespace(child, nameSpace);
            }
        }
    
        3
  •  1
  •   AnthonyWJones    17 年前

    问题是用于创建XElement的XName需要指定正确的命名空间。我想做的是创建一个这样的静态类:-

    public static class XHtml
    {
        public static readonly XNamespace Namespace = "http://www.w3.org/1999/xhtml";
        public static XName Html { get { return Namespace + "html"; } }
        public static XName Body { get { return Namespace + "body"; } }
                  //.. other element types
    }
    

    现在,您可以像这样构建一个xhtml文档:-

    XDocument doc = new XDocument(
        new XElement(XHtml.Html,
            new XElement(XHtml.Body)
        )
    );
    

    该静态类的另一种方法是:-

    static class XHtml
    {
        public static readonly XNamespace Namespace = "http://www.w3.org/1999/xhtml";
        public static readonly XName Html = Namespace + "html";
        public static readonly XName Body = Namespace + "body";
    }
    

    这有一个缺点,即实例化所有可能的XName,无论您是否使用它们,但好处是Namespace+“标记名”的转换只发生一次。否则,我不确定这种转换是否会得到优化。我确信XNames只实例化一次:-

    XNamepace n = "http://www.w3.org/1999/xhtml";
    XNames x = n + "A";
    XName y = n + "A";
    Object.ReferenceEquals(x, y) //is true.
    
    推荐文章