我正在使用包含类似于此的结构的XML文档:
<MT>
<Events>
<event id="1">
<field name="blah" value="a_value" type="atype" />
.
.
.
</event>
</Events>
</MT>
Run Code Online (Sandbox Code Playgroud)
我目前正以这种方式将文件从文件加载到XML文档中:
XmlDocument xdoc = new XmlDocument();
xdoc.Load("somefile.xml"); //Successfully loads btw
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试运行下一行代码时,我遇到了一个问题,只有这个特定的文档:
xdoc.SelectSingleNode("//event[@id='1']"); //This returns a null
Run Code Online (Sandbox Code Playgroud)
我是否在正确的轨道上猜测这是因为使用名为'id'的属性的问题或者我在代码中遗漏了某些内容而返回null?
在我无法控制的情况下,我有一个XmlDocument,它具有如下结构:
<parent1>
...minor amount of data...
</parent1>
Run Code Online (Sandbox Code Playgroud)
我有另一个XmlDocument,也在我的控制之外,它具有以下结构:
<parent2>
..very large amount of data...
</parent2>
Run Code Online (Sandbox Code Playgroud)
我需要一个XmlDocument格式:
<parent1>
...minor amount of data...
<parent2>
..very large amount of data...
</parent2>
</parent1>
Run Code Online (Sandbox Code Playgroud)
我不想复制parent2.如何在不复制parent2的情况下获得所需的结构?我相信这意味着
oParent1.DocumentElement.AppendChild(oParent1.ImportNode(oParent2.DocumentElement, true));
Run Code Online (Sandbox Code Playgroud)
是不可能的.
有什么好的解决方案吗?
无论如何,使用XMLDocument.Save()创建的文件的另一个进程监视可能会遇到部分文件?如果Save()覆盖现有文件会有什么不同吗?
我需要获取XmlNodeList,其中节点名称包含"mystring"
XML
<?xml version="1.0" encoding="utf-8"?>
<root>
<node1>
node1 value
</node1>
<node2_mystring>
node2 value
</node2_mystring>
<node3>
node3 value
</node3>
<node4_mystring>
node 4 value
</node4_mystring>
</root>
Run Code Online (Sandbox Code Playgroud)
期望的输出是
<?xml version="1.0" encoding="utf-8"?>
<root>
<node2_mystring>
node2 value
</node2_mystring>
<node4_mystring>
node 4 value
</node4_mystring>
</root>
Run Code Online (Sandbox Code Playgroud)
我试过类似的东西 XmlNodeList mystringElements = xmlDocument.SelectNodes(@"//*[contains(name,'mystring')]");
但它返回零节点.我应该在XPath查询中放置什么来实现这一目标.
有没有办法在XML文档中包含多种语言的示例(例如C#和Visual Basic)?
我正在使用SandCastle来构建MSDN样式的文档,并希望包含几种.NET语言的用法示例.
我需要将一个XmlDocument文件保存到适当的缩进文件中,(Formatting.Indented)但是一些带有子项的节点必须在一行中(Formatting.None).
如何实现这一点,因为XmlTextWriter接受整个文档的设置?
在@Ahmad Mageed的resposne之后编辑:
我不知道在写入过程中可以修改XmlTextWriter设置.那是好消息.
现在我正在以这种方式保存xmlDocument(已经填充了节点,具体是.xaml页面):
XmlTextWriter writer = new XmlTextWriter(filePath, Encoding.UTF8);
writer.Formatting = Formatting.Indented;
xmlDocument.WriteTo(writer);
writer.Flush();
writer.Close();
Run Code Online (Sandbox Code Playgroud)
当然,它可以在所有节点中实现缩进.我需要在处理所有<Run>节点时禁用缩进.
在您的示例中,您"手动"写入XmlTextWriter.有没有一种简单的方法来爬行所有xmlDocument节点并将它们写入XmlTextWriter,以便我可以检测<Run>节点?或者我是否必须编写某种递归方法,这种方法将转发给当前节点的每个子节点?
我的应用程序使用 XmlDocument 生成 XML。一些数据包含换行符和回车符。
当像这样将文本分配给 XmlElement 时:
e.InnerText = "Hello\nThere";
Run Code Online (Sandbox Code Playgroud)
生成的 XML 如下所示:
<e>Hello
There</e>
Run Code Online (Sandbox Code Playgroud)
XML 的接收者(我无法控制)将换行符视为空白,并将上述文本视为:
"Hello There"
Run Code Online (Sandbox Code Playgroud)
为了让接收者保留换行符,它需要编码为:
<e>Hello
There</e>
Run Code Online (Sandbox Code Playgroud)
如果数据应用于 XmlAttribute,则换行符将被正确编码。
我尝试使用 InnerText 和 InnerXml 将文本应用于 XmlElement,但两者的输出相同。
有没有办法让 XmlElement 文本节点以其编码形式输出换行符和回车符?
下面是一些示例代码来演示这个问题:
string s = "return[\r] newline[\n] special[&<>\"']";
XmlDocument d = new XmlDocument();
d.AppendChild( d.CreateXmlDeclaration( "1.0", null, null ) );
XmlElement r = d.CreateElement( "root" );
d.AppendChild( r );
XmlElement e = d.CreateElement( "normal" );
r.AppendChild( e );
XmlAttribute a = d.CreateAttribute( "attribute" );
e.Attributes.Append( a );
a.Value = …Run Code Online (Sandbox Code Playgroud) 我正在尝试针对现有的 XmlSchemaSet 验证传入的输入 xmlDocument。以下是代码:
public class ValidateSchemas
{
private bool _isValid = true;
public List<string> errorList = new List<string>();
public bool ValidateDocument(XmlDocument businessDocument)
{
XmlSchemaSet schemaSet = SchemaLoader.Loader();
bool isValid = Validate(businessDocument, SchemaLoader._schemaSet);
return isValid;
}
public bool Validate(XmlDocument document, XmlSchemaSet schema)
{
ValidationEventHandler eventHandler = new ValidationEventHandler(HandleValidationError);
document.Schemas = schema;
document.Validate(eventHandler);
return _isValid;
}
private void HandleValidationError(object sender, ValidationEventArgs ve)
{
_isValid = false; errorList.Add(ve.Message);
}
}
Run Code Online (Sandbox Code Playgroud)
从验证的角度来看,代码运行良好。但是 errorList 仅捕获第一个节点错误。它不会捕获其他节点错误。看起来该事件仅被触发一次。如何做到这一点,请帮助。请注意,我将 xmldocument 作为输入,因此没有使用阅读器。
我正在制作一个Web应用程序,它从网站获取RSS提要(URL在数据库中),然后将它们加载到我的Web应用程序中.但是我收到了这个错误
System.Xml.XmlException:缺少根元素.root元素丢失了.at line:rssdoc.load(rssStream);
异常详细信息:System.Xml.XmlException:有多个根元素.第2行,第2位.所以如何通过单个xml元素封装其他所有内容
这是我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Net;
using System.Text;
using System.IO;
using System.Data;
using System.Data.SqlClient;
public partial class poletics : System.Web.UI.Page
{
public SqlConnection oldbcon = new SqlConnection();
static int n = 0;
static DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
oldbcon = opncon();
using (oldbcon)
{
SqlDataAdapter ad = new SqlDataAdapter("SELECT * from LebPolRss", oldbcon);
ad.Fill(dt);
}
int f = …Run Code Online (Sandbox Code Playgroud) 从Windows 10 build 10240在Microsoft Edge上测试.在构建10586中修复.
概要
XMLDocument.prototype.evaluate在已namespaceURI设置为null崩溃Microsoft Edge中的当前选项卡进程的文档上运行,使该选项卡的开发人员工具无响应,向其发送调试信息watson.telemetry.microsoft.com并强制重新加载页面.
摄制
要重现,请打开Microsoft Edge中的任何网站,按F12打开开发人员工具,选择控制台,然后运行以下3行javascript:
var doc = document.implementation.createDocument(null, null, null);
var node = doc.createElement('A');
doc.evaluate('B', node, doc.createNSResolver(doc), 9, null);
Run Code Online (Sandbox Code Playgroud) xmldocument ×10
c# ×9
xml ×4
.net ×3
.net-2.0 ×1
c#-2.0 ×1
indentation ×1
javascript ×1
optimization ×1
rss ×1
sandcastle ×1
vb.net ×1
xpath ×1