我在WPF中导出控件的XAML代码,用的是XElement元素,现在的问题是导出的xaml里,根元素除外,其它的节点,只要不是叶子节点,或者说只要节点里面还包含了其它的节点,都会多出来一个xmlns="",不知道什么原因,求指教。<UserControl xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/up-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
  <Grid Width="147.00000000000003" Background="#FF808080" Height="200.00000000000003" xmlns="">
    <Button Canvas.Left="27" Canvas.Top="54" Panel.ZIndex="1" Width="65" Height="65" Content="按钮" />
  </Grid>
</UserControl>Grid的属性里,多了一个xmlns="",有人知道它是怎么生成的吗,怎么去掉?如果Button下面也有子元素,那么Button的属性也会出来这个东西。

解决方案 »

  1.   

    http://zhidao.baidu.com/question/320768364.html
      

  2.   

    http://zxlruby.blog.163.com/blog/static/22583152201131110589137/这个是处理
      

  3.   

    private XElement SerializeXaml(IEnumerable<DesignerItem> designerItems, XElement xaml)
            {
                string strCanvas = "Canvas";//Canvas控件名称
                XElement xReturn = xaml;           
                XElement xTemp = null;            double dLeft = 0.0; //记录Canvas的左边距
                double dTop = 0.0;  //记录Canvas的上边距
                foreach (var item in designerItems)
                {
                    if (item.Content.GetType().Name.ToString() == strCanvas)//如果是Canvas控件,添加到根元素
                    {
                        dLeft = Canvas.GetLeft(item);
                        dTop = Canvas.GetTop(item);                    xTemp = new XElement(item.Content.GetType().Name.ToString(),//控件
                                new XAttribute("Width", item.Width),
                                new XAttribute("Background", item.Background != null ? item.Background : new SolidColorBrush(Colors.Gray)),
                                new XAttribute("Height", item.Height));
                        xReturn.Add(xTemp);
                    }
                    else if (xReturn.Elements(strCanvas) != null)//如果不是Canvas控件,则将当前控件添加到Canvas控件
                    {
                        double bLeft = Canvas.GetLeft(item) - dLeft;//计算当前控件相对Canvas的宽度
                        double bTop = Canvas.GetTop(item) - dTop; //计算当前控件相对Canvas的高度
                        
                        xTemp = new XElement(item.Content.GetType().Name.ToString(),//控件
                                new XAttribute("Canvas.Left", bLeft),
                                new XAttribute("Canvas.Top", bTop),
                                new XAttribute("Panel.ZIndex", Panel.GetZIndex(item)),
                                new XAttribute("Width", item.Width),
                                new XAttribute("Height", item.Height),
                                new XAttribute("Content", item.Content != null ? item.Content.ToString().Split(':')[1].Trim() : String.Empty));                    xReturn.Element(strCanvas).Add(xTemp);
                    }
                    else
                        continue;
                }
                return xReturn;
            }主要代码是这个方法。
    当执行完xReturn.Add(xTemp);后,Grid元素属性里就多了一个xmlns="",不解。
      

  4.   

    new XElement(ns+"Grid")的时候,ns定义成 http://schemas.microsoft.com/winfx/2006/xaml/presentation
      

  5.   

    ns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation";
    xTemp = new XElement(ns+item.Content.GetType().Name.ToString(),//控件
      

  6.   


    你好,你明白我的意思了。其实这个xaml的命名空间我在传入的参数xaml里面已经定义了,在Gird的元素属性里不想出现这个xmlns属性,他自己多出来的,我没设置。怎么办?
      

  7.   


    顶一下。
    用了6楼的方法后,是消失了,但是xReturn.Element(strCanvas).Add(xTemp);执行不了,xReturn.Element(strCanvas)为null。
      

  8.   


    你好,用了这个方法后是消失了,但是添加子元素时报错,xReturn.Element(strCanvas).Add(xTemp)中xReturn.Element(strCanvas)为NULL
      

  9.   

    如果根节点写了
    <UserControl  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">这样的名称空间定义,下面的所有子节点默认的名称空间都是http://schemas.microsoft.com/winfx/2006/xaml/presentation的
    包括查询,都要加名称空间的。
    为NULL说明你的查询方法是错误的。丢了名称空间名称空间是xml中最基本的概念,搞xml,这个概念一定要清楚
      

  10.   

    谢了,昨天在xReturn.Element(strCanvas).Add(xTemp)加入命名空间后就可查到了。万分感谢!