【问题标题】:How to remove "xmlns:y" in this case?在这种情况下如何删除“xmlns:y”?
【发布时间】:2012-11-14 02:30:41
【问题描述】:

我想制作下面的 graphml(xml) 文件。

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<graphml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:y="http://www.yworks.com/xml/graphml" schemaLocation="http://graphml.graphdrawing.org/xmlns http://www.yworks.com/xml/schema/graphml/1.1/ygraphml.xsd" xmlns="http://graphml.graphdrawing.org/xmlns">
  <key for="node" id="d3" yfiles.type="nodegraphics" />
  <graph edgedefault="directed" id="G">
    <node id="3">
      <data key="d3">
        <y:ShapeNode>
          <y:Geometry height="50" width="50" x="80" y="0" />
          <y:BorderStyle color="#000000" type="line" width="1.0" />
          <y:NodeLabel alignment="center" autoSizePolicy="content">TEST</y:NodeLabel>
        </y:ShapeNode>
      </data>
    </node>
    <node id="1000">
      <data key="d3">
        <y:ShapeNode>
          <y:Geometry height="50" width="50" x="240" y="0" />
          <y:BorderStyle color="#000000" type="line" width="1.0" />
          <y:NodeLabel alignment="center" autoSizePolicy="content">TEST</y:NodeLabel>
        </y:ShapeNode>
      </data>
    </node>
  </graph>
</graphml>

但我的代码制作了以下文件

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<graphml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:y="http://www.yworks.com/xml/graphml" schemaLocation="http://graphml.graphdrawing.org/xmlns http://www.yworks.com/xml/schema/graphml/1.1/ygraphml.xsd" xmlns="http://graphml.graphdrawing.org/xmlns">
  <key for="node" id="d3" yfiles.type="nodegraphics" />
  <graph edgedefault="directed" id="G">
    <node id="3">
      <data key="d3">
        <y:ShapeNode xmlns:y="http://graphml.graphdrawing.org/xmlns">
          <y:Geometry height="50" width="50" x="80" y="0" />
          <y:BorderStyle color="#000000" type="line" width="1.0" />
          <y:NodeLabel alignment="center" autoSizePolicy="content">TEST</y:NodeLabel>
        </y:ShapeNode>
      </data>
    </node>
    <node id="1000">
      <data key="d3">
        <y:ShapeNode xmlns:y="http://graphml.graphdrawing.org/xmlns">
          <y:Geometry height="50" width="50" x="240" y="0" />
          <y:BorderStyle color="#000000" type="line" width="1.0" />
          <y:NodeLabel alignment="center" autoSizePolicy="content">TEST</y:NodeLabel>
        </y:ShapeNode>
      </data>
    </node>
  </graph>
</graphml>

Graphml 程序不支持输出文件,因为文件有部分 "xmlns:y="http://graphml.graphdrawing.org/xmlns" 在&lt;y:ShapeNode xmlns:y="http://graphml.graphdrawing.org/xmlns"&gt;.

我想删除部分“xmlns:y="http://graphml.graphdrawing.org/xmlns"

<y:ShapeNode xmlns:y="http://graphml.graphdrawing.org/xmlns">

如何擦除部分?我该怎么办? 我的源代码如下

    XDocument xDoc = new XDocument();
    XNamespace graphmlNamespace = "http://graphml.graphdrawing.org/xmlns";
    XNamespace xsi = XNamespace.Get("http://www.w3.org/2001/XMLSchema-instance");


    XElement mainElement = new XElement(graphmlNamespace + "graphml");
    XAttribute mainAttrib2 = new XAttribute(XNamespace.Xmlns + "xsi", xsi.NamespaceName);
    XAttribute mainAttrib3 = new XAttribute(XNamespace.Xmlns + "y", "http://www.yworks.com/xml/graphml");
    XAttribute mainAttrib4 = new XAttribute("schemaLocation", "http://graphml.graphdrawing.org/xmlns http://www.yworks.com/xml/schema/graphml/1.1/ygraphml.xsd");
    mainElement.Add(mainAttrib2);
    mainElement.Add(mainAttrib3);
    mainElement.Add(mainAttrib4);

    XElement subElement_key = new XElement(graphmlNamespace + "key");
    XAttribute subAttrib_key1 = new XAttribute("for", "node");
    XAttribute subAttrib_key2 = new XAttribute("id", "d3");
    XAttribute subAttrib_key3 = new XAttribute("yfiles.type", "nodegraphics");

    subElement_key.Add(subAttrib_key1);
    subElement_key.Add(subAttrib_key2);
    subElement_key.Add(subAttrib_key3);

    mainElement.Add(subElement_key);

    //
    XElement subElement_graph = new XElement(graphmlNamespace + "graph");
    XAttribute subAttrib_graph1 = new XAttribute("edgedefault", "directed");
    XAttribute subAttrib_graph2 = new XAttribute("id", "G");

    subElement_graph.Add(subAttrib_graph1);
    subElement_graph.Add(subAttrib_graph2);

    mainElement.Add(subElement_graph);

    //
    for( int i=0; i< 2; i++) // for test
    {
        XElement sub2Element_node = new XElement(graphmlNamespace + "node");
        XAttribute sub2Attrib_node1 = new XAttribute("id", row["SkillEnumID"].ToString());
        sub2Element_node.Add(sub2Attrib_node1);

        subElement_graph.Add(sub2Element_node);

        // <data key="d3">
        XElement sub3Element_data = new XElement(graphmlNamespace + "data");
        XAttribute sub3Attrib_data1 = new XAttribute("key", "d3");
        sub3Element_data.Add(sub3Attrib_data1);
        sub2Element_node.Add(sub3Element_data);


        //<y:ShapeNode>
        XElement sub4Element_yShapeNode = new XElement(graphmlNamespace + "ShapeNode");
        XAttribute sub4Attrib_yShapeNode1 = new XAttribute(XNamespace.Xmlns + "y", graphmlNamespace);
        sub4Element_yShapeNode.Add(sub4Attrib_yShapeNode1);



        sub3Element_data.Add(sub4Element_yShapeNode);

        //<y:Geometry height="30.0" width="30.0" x="-15.0" y="-15.0"/>
        int iX = 0;
        Int32.TryParse(row["ColumnNumber"].ToString(), out iX);
        iX = (iX * (m_iNodeWidth + m_iXGap));

        int iY = 0;
        Int32.TryParse(row["RowNumber"].ToString(), out iY);
        iY = (iY * (m_iNodeHeight + m_iYGap));

        XElement sub5Element_yGeometry = new XElement(graphmlNamespace + "Geometry");
        XAttribute sub5Attrib_yGeometry1 = new XAttribute("height", m_iNodeHeight.ToString());
        XAttribute sub5Attrib_yGeometry2 = new XAttribute("width", m_iNodeWidth.ToString());
        XAttribute sub5Attrib_yGeometry3 = new XAttribute("x", iX.ToString());
        XAttribute sub5Attrib_yGeometry4 = new XAttribute("y", iY.ToString());
        sub5Element_yGeometry.Add(sub5Attrib_yGeometry1);
        sub5Element_yGeometry.Add(sub5Attrib_yGeometry2);
        sub5Element_yGeometry.Add(sub5Attrib_yGeometry3);
        sub5Element_yGeometry.Add(sub5Attrib_yGeometry4);

        sub4Element_yShapeNode.Add(sub5Element_yGeometry);

        // <y:Fill color="#FFCC00" transparent="false"/>
        XElement sub5Element_yBorderStyle = new XElement(graphmlNamespace + "BorderStyle");
        XAttribute sub5Attrib_yBorderStyle1 = new XAttribute("color", "#000000");
        XAttribute sub5Attrib_yBorderStyle2 = new XAttribute("type", "line");
        XAttribute sub5Attrib_yBorderStyle3 = new XAttribute("width", "1.0");
        sub5Element_yBorderStyle.Add(sub5Attrib_yBorderStyle1);
        sub5Element_yBorderStyle.Add(sub5Attrib_yBorderStyle2);
        sub5Element_yBorderStyle.Add(sub5Attrib_yBorderStyle3);

        sub4Element_yShapeNode.Add(sub5Element_yBorderStyle);

        //<y:NodeLabel alignment="center" autoSizePolicy="content">10 테스트</y:NodeLabel>
        string strLabel = "TEST";

        XElement sub5Element_yNodeLabel = new XElement(graphmlNamespace + "NodeLabel");
        XAttribute sub5Attrib_yNodeLabel1 = new XAttribute("alignment", "center");
        XAttribute sub5Attrib_yNodeLabel2 = new XAttribute("autoSizePolicy", "content");
        sub5Element_yNodeLabel.Add(sub5Attrib_yNodeLabel1);
        sub5Element_yNodeLabel.Add(sub5Attrib_yNodeLabel2);
        sub5Element_yNodeLabel.Value = strLabel;

        sub4Element_yShapeNode.Add(sub5Element_yNodeLabel);
    }

    //

    xDoc.Add(mainElement);

    xDoc.Declaration = new XDeclaration("1.0", "utf-8", "no");
    xDoc.Save(strFileFullPath);

【问题讨论】:

  • 两个 XML 示例在我看来都一样...你能把区别说得更清楚些吗?
  • 区别是。第一个文件只有 ,但另一个文件有 graphml.graphdrawing.org/xmlns">。
  • yEd(Graphml 应用程序)无法正常加载我的文件,因为 中的部分 xmlns:y=~~~~

标签: c# .net xml linq graphml


【解决方案1】:

只需删除这两行:

XAttribute sub4Attrib_yShapeNode1 = new XAttribute(XNamespace.Xmlns + "y", graphmlNamespace);
sub4Element_yShapeNode.Add(sub4Attrib_yShapeNode1);

您的 XML 已包含已定义的默认命名空间:http://graphml.graphdrawing.org/xmlnsShapeNode 已在此命名空间中定义。

【讨论】:

  • 我不确定它是否有帮助,因为节点需要位于 2 个命名空间中,而代码中只定义了一个......但你可能是对的 - 示例代码太多当然...
  • @AlexeiLevenkov,你是什么意思?根据提供的 XML,ShapeDatahttp://graphml.graphdrawing.org/xmlns 中定义。所以不需要在其中指定命名空间,因为 XML 的默认命名空间为http://graphml.graphdrawing.org/xmlns。根据 XML 规范,两个 XML 都是正确的,所以这是 OP 的 XML 解析器的问题。
  • y:ShapeNode 在第一个 XML 中具有“yworks.com/xml/graphml”作为命名空间...而不是使用空命名空间前缀的“graphml.graphdrawing.org/xmlns”。
【解决方案2】:

为什么要将 graphmlNamespace 初始化为与您想要的不同的值?如果您希望元素位于命名空间 http://www.yworks.com/xml/graphml 中,则将它们放在该命名空间中,而不是 http://graphml.graphdrawing.org/xmlns 中。

【讨论】:

    【解决方案3】:

    您明确地将 y 重新定义为其他 (graphmlNamespace) 命名空间的前缀。 ShapNode 和其他带有 y: 前缀的节点应该在 "http://www.yworks.com/xml/graphml" 命名空间中。您需要另一个 XNamespace 用于代码中的该命名空间/前缀。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-17
      • 1970-01-01
      • 2013-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-19
      相关资源
      最近更新 更多