【问题标题】:Read XElement Fragment from xml file to XElement Element将 XElement 片段从 xml 文件读取到 XElement 元素
【发布时间】:2015-08-13 11:08:45
【问题描述】:

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

我怎样才能读取那个恰好代表 XElement 元素的 xml 片段?

    <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"); 的方式这根本不起作用。

感谢您的提示

【问题讨论】:

  • 您已经接近了,但您的 XmlTextReader 期望 XML 字符串不是 XML 字符串的路径。
  • 谢谢,你的提示解决了我的问题

标签: c# xml xelement


【解决方案1】:

你可以这样做

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.txt";
        static void Main(string[] args)
        {
            string input = File.ReadAllText(FILENAME);
            XElement element = XElement.Parse("<Root xmlns:y=\"www.mynamespace.com\"></Root>");

            element.Add(input);

        }

    }
}
​

【讨论】:

  • 你试过了吗?所有这一切都会在Root 中添加大量文本。你最终会得到类似&lt;Root xmlns:y="www.mynamespace.com"&gt; &amp;lt;node id="n0::n0"&amp;gt; ...
  • 不,我没有尝试,我现在正在使用流式阅读器来获取文本,然后它就可以工作了。但你的方式似乎短了很多。当我阅读下一个 xml 片段时,我会尝试一下。
猜你喜欢
  • 2013-04-17
  • 1970-01-01
  • 2021-05-07
  • 1970-01-01
  • 1970-01-01
  • 2011-05-10
  • 2011-09-14
  • 1970-01-01
  • 2012-10-05
相关资源
最近更新 更多