我定义了一个函数public static XmlAttribute InsertAttribute(XmlNode oTargetNode, string sAttributeName, string sAttributeValue)
{
XmlNode attr = oTargetNode.OwnerDocument.CreateNode(XmlNodeType.Attribute, sAttributeName,"");
attr.Value = sAttributeValue;
oTargetNode.Attributes.SetNamedItem(attr);
return (System.Xml.XmlAttribute)attr;
}现在当sAttributeName带有 ":" 号的时候,他将解析为 Prefix 和 LocalName 两部分,这时候插入的那个属性就是 LocalName部分了,比如:
XmlElement elem = oParentNode.OwnerDocument.CreateElement("image",sXMLNamespace);
InsertAttribute(elem,"xlink:href",sSrc); //这里我就是要插入"xlink:href",可是得到的是href
InsertAttribute(elem,"x",fX.ToString(sDecimalFormat));
InsertAttribute(elem,"y",fY.ToString(sDecimalFormat));
InsertAttribute(elem,"width",fWidth.ToString(sDecimalFormat));
InsertAttribute(elem,"height",fHeight.ToString(sDecimalFormat));我该怎么办?
下面是我得到的东西:
<image x="685" y="371" width="25" height="34" href="../images/egend.jpg" id="PieColor" />
                                              ~~~~~
                                              这里应该是xlink:href,可是被解析成href了

解决方案 »

  1.   

    你说的是我的那个函数不能执行吗?可以执行的,我就是xlink:href会被转成href.
      

  2.   

    CreateNode 用法帮助里面有啊ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.2052/cpref/html/frlrfsystemxmlxmldocumentclasscreatenodetopic2.htm
      

  3.   

    我的意思是,如果上下文没有定义xlink前缀,在这里肯定没法执行
    如果已经定义了,那么这里就再没有必要写xlink:href,因为默认就是xlink:href如果非得在已经定义了xlink的前提下,还得让其输出xlink:href,可以这样:
    public static XmlAttribute InsertAttribute(XmlNode oTargetNode, string sAttributeName, string sAttributeValue, string ns)
    {
     XmlNode attr = oTargetNode.OwnerDocument.CreateAttribute(sAttributeName,ns);
     attr.Value = sAttributeValue;
     oTargetNode.Attributes.SetNamedItem(attr);
     return (System.Xml.XmlAttribute)attr;
    }
    调用时:
    // 比如某一个父结点定义了xmlns:xlink="http://www.xlink.org"
    string ns = "http://www.xlink.org";
    InsertAttribute(elem,"xlink:href",sSrc,ns);
      

  4.   

    如果已经定义了,那么这里就再没有必要写xlink:href,因为默认就是xlink:href
    >>>
    还是有必要地,俺也糊涂了 :P
      

  5.   

    谢谢,用你的方法是可以加xlink:href了,应该是命名空间的问题,可是我还是没弄懂我那个问题,帮我看看下面的SVG文件是什么问题:<?xml version="1.0" encoding="utf-8"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11-flat-20030114.dtd"[]><svg width="744" height="1052" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ypsoft="http://www.perfectsvg.com/ypsoft" xml:space="preserve">
    <image x="68" y="109" width="182" height="112" xlink:href="109330923398697.jpg" stroke="#000000" />
    </svg>这个文件是正常的,可是当我去掉了xlink就看不到图片了,请问我该怎么定义xlink呢?
    用你的方法,它会在后面加 xmlns:xlink="xlink" ,也还是没法看到图片。
      

  6.   

    不懂SVG :P
    孟子应该会这个
      

  7.   

    呵呵,那就谢谢你了!昨天发帖的时候多发了一帖,
    http://community.csdn.net/Expert/TopicView3.asp?id=3927237
    我也是今天早上才看到的,昨天发的时候还不知道自己多发了,呵呵,你Up一下,我一起揭帖,要不然那个帖子结不了。
    虽然分数对你可能没什么,但这也是我现在唯一能表示我感谢的东西了。哈哈。