从xml文件中读取XElement片段到XElement元素

时间:2015-08-13 11:08:45

标签: c# xml xelement

我在xml-File中有xml片段。该片段具有带命名空间的标签。

如何读取xml片段,它完全代表XElement元素?

    <node id="n0::n0">
  <data key="d6">
    <y:ShapeNode>
      <y:Geometry height="91.44" width="59.49119999999999" x="364.256180835285" y="-698.4404365079365"/>
      <y:Fill color="#FFCC00" transparent="false"/>
      <y:BorderStyle color="#000000" type="line" width="1.0"/>
      <y:NodeLabel alignment="center" autoSizePolicy="content" fontFamily="Dialog" fontSize="12" fontStyle="plain" hasBackgroundColor="false" hasLineColor="false" height="18.701171875" modelName="internal" modelPosition="t" textColor="#000000" visible="true" width="48.677734375" x="5.406732812499968" y="4.0">MELEIN</y:NodeLabel>
      <y:NodeLabel alignment="center" autoSizePolicy="content" fontFamily="Dialog" fontSize="12" fontStyle="plain" hasBackgroundColor="false" hasLineColor="false" height="18.701171875" modelName="custom" textColor="#000000" visible="true" width="27.35546875" x="16.067865624999968" y="20.843814062500087">8,00<y:LabelModel>
          <y:SmartNodeLabelModel distance="4.0"/>
        </y:LabelModel>
        <y:ModelParameter>
          <y:SmartNodeLabelModelParameter labelRatioX="0.0" labelRatioY="-0.5" nodeRatioX="0.0" nodeRatioY="-0.2720492775317138" offsetX="0.0" offsetY="0.0" upX="0.0" upY="-1.0"/>
        </y:ModelParameter>
      </y:NodeLabel>
      <y:Shape type="rectangle"/>
    </y:ShapeNode>
  </data>
</node>

我尝试了很多不同的方式,最后一个接近目标

        var mngr = new System.Xml.XmlNamespaceManager(new System.Xml.NameTable());
    mngr.AddNamespace(string.Empty, "urn: ignore"); // or proper URL
    mngr.AddNamespace("y", "urn:ignore"); // or proper URL
    var parserContext = new System.Xml.XmlParserContext(null, mngr, null, System.Xml.XmlSpace.None, null);

    var txtReader = new System.Xml.XmlTextReader("block.graphml", System.Xml.XmlNodeType.Element, parserContext);
    var ele = XElement.Load(txtReader);

但它在System.InvalidOperationException

的最后一行崩溃了

有没有简单的方法将xml片段导入现有的xelement? 我更喜欢XElement.load("block.graphml");的方式。这根本不起作用。

感谢您的提示

0 个答案:

没有答案