body
在下面的代码中获取混合元素内容的最佳方法是什么?该元素可能包含XHTML或文本,但我只想要其字符串形式的内容.该XmlElement
类型具有InnerXml
我正在追求的属性.
编写的代码几乎可以实现我想要的功能,但包含了我不想要的周围的<body>
... </body>
元素.
XDocument doc = XDocument.Load(new StreamReader(s));
var templates = from t in doc.Descendants("template")
where t.Attribute("name").Value == templateName
select new
{
Subject = t.Element("subject").Value,
Body = t.Element("body").ToString()
};
Run Code Online (Sandbox Code Playgroud) 我需要answer
在这一点XML中获取HTML内容:
<qa>
<question>Who are you?</question>
<answer>Who who, <strong>who who</strong>, <em>me</em></answer>
</qa>
Run Code Online (Sandbox Code Playgroud)
所以我希望得到一个字符串"Who who,<strong>谁是</ strong>,<em> me </ em>".
如果我有answer
a SimpleXMLElement
,我可以打电话asXML()
来"<answer>谁是谁,<strong>谁</ strong>,<em>我</ em> </ answer>",但是如何获得内部XML没有元素本身缠绕的元素?
我更喜欢不涉及字符串函数的方法,但如果这是唯一的方法,那就这样吧.
我遇到了一个奇怪的情况,希望有一个比我了解得更多的人可以帮助我解决这个问题。
我正在将图像插入Xml文档中,以便可以使用Microsoft Word打开它。作为其中的一部分,我需要添加一个映射到包含图像的元素的Xml'Relationship'。直截了当。
我添加的节点应如下所示:
<Relationship Id="rId6" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/image1.png" />
Run Code Online (Sandbox Code Playgroud)
但是,在最终的.doc文件中,该行显示如下:
<Relationship Id="rId6" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/image1.png" xmlns="" />
Run Code Online (Sandbox Code Playgroud)
即它现在具有一个空的xmlns =“”属性。
这足以让Word相信文档已损坏并拒绝打开。如果我手动打开文件并删除该属性,则会打开文件。
显然,我想以编程方式删除它:-)所以我找到了父节点。这是我的理解有些模糊的地方。我相信OuterXml元素包含节点及其所有子元素的内容,而InnerXml仅包含子元素。
这就是我所看到的(请注意,转义字符是因为我是从Visual Studio的文本查看器中剪切下来的)。
外层Xml:
"<Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\">
<Relationship Id=\"rId3\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/webSettings\" Target=\"webSettings.xml\" />
<Relationship Id=\"rId2\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings\" Target=\"settings.xml\" />
<Relationship Id=\"rId1\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles\" Target=\"styles.xml\" />
<Relationship Id=\"rId5\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme\" Target=\"theme/theme1.xml\" />
<Relationship Id=\"rId4\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable\" Target=\"fontTable.xml\" />
<Relationship Id=\"rId6\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image\" Target=\"media/image1.png\" xmlns=\"\" />
</Relationships>"
Run Code Online (Sandbox Code Playgroud)
InnerXml:
"<Relationship Id=\"rId3\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/webSettings\" Target=\"webSettings.xml\" xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\" />
<Relationship Id=\"rId2\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings\" Target=\"settings.xml\" xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\" />
<Relationship Id=\"rId1\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles\" Target=\"styles.xml\" xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\" />
<Relationship Id=\"rId5\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme\" Target=\"theme/theme1.xml\" …
Run Code Online (Sandbox Code Playgroud) XmlDocument doc = new XmlDocument();
doc.AppendChild(doc.CreateElement("Foo"));
doc.DocumentElement.InnerXml = "Test";
StringBuilder result = new StringBuilder();
doc.WriteContentTo(XmlWriter.Create(result));
Run Code Online (Sandbox Code Playgroud)
最后,结果是:
<Foo>Test
Run Code Online (Sandbox Code Playgroud)
这意味着缺少最终元素.为什么这样,我该如何解决?