我需要将一个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>节点?或者我是否必须编写某种递归方法,这种方法将转发给当前节点的每个子节点?
我有以下代码,我需要从VS 2003(.Net 1.1)迁移到VS 2008(.Net 2+),但我得到编译错误 -
System.Xml.Xsl.XslTransform'已过时:已弃用此类.我可能需要使用System.Xml.Xsl.XslCompiledTransform.但我没有找到匹配的Load()和Transform()重载版本,我可以使用我的原始代码的所有参数.
在MSDN [如何:迁移您的XslTransform代码]我只发现了一些更简单的案例. http://msdn.microsoft.com/en-us/library/aa983475%28VS.80%29.aspx 但在我的代码中我看到一些评论暗示添加的参数用于避免异常,所以我更喜欢使用这些参数.
有人可以帮助迁移此代码吗?
谢谢,阿塔拉
' ----------------------------------------------------------------------
' VS 2003 code:
' ----------------------------------------------------------------------
. . .
Dim myXslDoc As Xml.XmlDocument
' ----------------------------------------------------------------------
Public Sub mcSetParameters(ByVal srcFileName As String)
' ----------------------------------------------------------------------
Me.myXslDoc = New Xml.XmlDocument
Me.myXslDoc.Load(srcFileName)
End Sub
' ----------------------------------------------------------------------
Public Sub mcSetHtml()
' ----------------------------------------------------------------------
Dim oXPathNav As System.Xml.XPath.XPathNavigator = xmlDoc.DocumentElement.CreateNavigator()
Dim sbContent As New System.Text.StringBuilder
Dim swContent As New System.IO.StringWriter(sbContent)
Dim args As New System.Xml.Xsl.XsltArgumentList
args.AddParam("paramName1", "", paramVal1.ToString)
args.AddParam("paramName2", "", paramVal2.ToString)
Try …Run Code Online (Sandbox Code Playgroud) 我的应用程序使用 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 作为输入,因此没有使用阅读器。
我有以以下开头的 XHTML 文件:
<html xmlns="http://www.w3.org/1999/xhtml">
Run Code Online (Sandbox Code Playgroud)
我加载它:
XmlDocument xml = new XmlDocument();
StringReader sr = new StringReader(html);
XmlTextReader xmltr = new XmlTextReader(sr);
xmltr.Namespaces = false;
xml.Load(xmltr);
Run Code Online (Sandbox Code Playgroud)
当我打电话时,xml.InnerXml我总是遇到The 'xmlns' attribute is bound to the reserved namespace 'http://www.w3.org/2000/xmlns/'.异常并且无法访问我的 XmlDocument 的内部 xml。如何在加载期间摆脱 xmlns?
解决方案是:
XmlNode xmln = xml.SelectSingleNode("//html");
if (xmln != null)
((XmlElement)xmln).RemoveAttribute("xmlns");
Run Code Online (Sandbox Code Playgroud) 一直在寻找答案,但找不到任何东西 - 我很新,所以也许我没有找到正确的关键词?
这是我正在使用的 XML 示例
<database>
<book>
<title>A</title>
<author>
<name>1</name>
</author>
</book>
<book>
<title>B</title>
<author>
<name>2</name>
</author>
<author>
<name>3</name>
</author>
<author>
<name>4</name>
</author>
<author>
<name>5</name>
</author>
</book>
</database>
Run Code Online (Sandbox Code Playgroud)
我尝试使用 C# XMLDocument 获取书籍 A 作者 1,然后获取书籍 B 作者 1、2、3、4、5
到目前为止,我正在使用的代码正在循环遍历所有作者,因此我得到了书 A 作者 1, 2, 3, 4, 5
到目前为止我的代码大致如下
XmlDocument doc = new XmlDocument();
doc.Load("myxmlfile");
XmlNode root = doc.SelectSingleNode("database");
XmlNodeList nodelist = root.SelectNodes("book");
foreach (XmlNode n in nodelist)
{
XmlNodeList authors = root.SelectNodes(".//author");
book.authorstring = "";
foreach (XmlNode author in authors) …Run Code Online (Sandbox Code Playgroud) 我XmlDocument在 C# 中有一个需要针对 Xml 架构进行验证的代码。我已经看过很多关于如何使用XmlDocument.Schemas.Add(namspace, xsdfilename). 谁能给我一些关于如何XmlDocument使用 XSD 字符串验证的指示?
我正在制作一个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) 我有一个与此设置类似的 xml 文档:
<invoice>
<IssueDate>2015-09-07</IssueDate>
<InvoiceType>380<InvoiceType>
<AccountingSupplierParty>
<Party>
<EndpointID></EndpointID>
<PartyName>
<Name>Company test</Name>
</PartyName>
</Party>
</AccountingSupplierParty>
</invoice>
Run Code Online (Sandbox Code Playgroud)
这只是整个 xml 文档的一小部分,只是为了展示文件的外观。
我也想检查所有元素,看看它们是否有空值,例如本例中的 EndpointID(我需要用 NA 替换空值)。
这是我到目前为止:
public static void AddToEmptyElements()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("testXml.xml");
XmlNodeList nodes = xmlDoc.DocumentElement.ChildNodes;
foreach (XmlNode node in nodes)
{
Console.WriteLine(node.Name);
}
}
Run Code Online (Sandbox Code Playgroud)
但是,此代码只会循环遍历根节点的子节点,而不是所有孙子节点(或曾孙子节点),例如<Party>和<EndpointID>元素。如何在循环中包含这些元素?
从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# ×8
xml ×7
.net ×2
.net-2.0 ×1
indentation ×1
javascript ×1
rss ×1
validation ×1
vb.net ×1
xmlnode ×1
xsd ×1