如何使用.NET实现此XML摘录

时间:2009-05-18 20:34:24

标签: .net xml

我正在尝试生成此XML语句(减去格式和特定​​值):

<swatchcolor RGB="c5c5c5">
  <sldcolorswatch:Optical 
    Ambient="0.520000" 
    Transparency="0.000000" 
    Diffuse="0.800000" 
    Specularity="1.000000" 
    Shininess="0.400000" 
    Emission="0.000000" />
</swatchcolor>

我正在使用这段代码:

Public Sub ToXml(ByVal writer As XmlWriter)
    writer.WriteStartElement("swatchcolor")
    writer.WriteAttributeString("RGB", _rgb)
    writer.WriteStartElement("sldcolorswatch", "Optical", My.Resources.xmlns)
    writer.WriteAttributeString("Ambient", _ambient.ToString("####################.000000"))
    writer.WriteAttributeString("Transparency", _transparency.ToString("####################.000000"))
    writer.WriteAttributeString("Diffuse", _diffuse.ToString("####################.000000"))
    writer.WriteAttributeString("Specularity", _specularity.ToString("####################.000000"))
    writer.WriteAttributeString("Shininess", _shininess.ToString("####################.000000"))
    writer.WriteAttributeString("Emission", _emission.ToString("####################.000000"))
    writer.WriteEndElement()
    writer.WriteEndElement()
End Sub

不幸的是,我得到了以下带有不需要的xmlns子句的XML:

<swatchcolor RGB="c5c5c5">
  <sldcolorswatch:Optical 
    Ambient=".520000" 
    Transparency=".000000" 
    Diffuse=".800000" 
    Specularity="1.000000" 
    Shininess=".400000" 
    Emission=".000000" 
    xmlns:sldcolorswatch="http://www.w3.org/2000/xmlns/" />
</swatchcolor>

如何摆脱“xmlns”?我知道它就在那里,因为我正在使用WriteStartElement的第三个参数,但是当我使用双参数版本时,它会将“光学”一词移到不适当的位置,如下所示:

<swatchcolor RGB="c5c5c5">
  <sldcolorswatch 
    Ambient=".520000" 
    Transparency=".000000" 
    Diffuse=".800000" 
    Specularity="1.000000" 
    Shininess=".400000" 
    Emission=".000000" 
    xmlns="Optical" />
</swatchcolor>

1 个答案:

答案 0 :(得分:1)

我想你想要:

writer.WriteStartElement("sldcolorswatch", "Optical", "http://www.solidworks.com/sldcolorswatch");