我使用.NET XmlReader打开XML文件并将文件保存在另一个文件名中,似乎DOCTYPE声明在两个文件之间发生了变化.虽然新保存的文件仍然是有效的XML,但我想知道它为什么坚持要更改原始标记.
Dim oXmlSettings As Xml.XmlReaderSettings = New Xml.XmlReaderSettings()
oXmlSettings.XmlResolver = Nothing
oXmlSettings.CheckCharacters = False
oXmlSettings.ProhibitDtd = False
oXmlSettings.IgnoreWhitespace = True
Dim oXmlDoc As XmlReader = XmlReader.Create(pathToOriginalXml, oXmlSettings)
Dim oDoc As XmlDocument = New XmlDocument()
oDoc.Load(oXmlDoc)
oDoc.Save(pathToNewXml)
Run Code Online (Sandbox Code Playgroud)
以下(原始文件中):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd">
Run Code Online (Sandbox Code Playgroud)
成为(注意结尾处的[]字符):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd"[]>
Run Code Online (Sandbox Code Playgroud) 我正在尝试访问UPS跟踪信息,并且根据他们的示例,我需要构建一个这样的请求:
<?xml version="1.0" ?>
<AccessRequest xml:lang='en-US'>
<AccessLicenseNumber>YOURACCESSLICENSENUMBER</AccessLicenseNumber>
<UserId>YOURUSERID</UserId>
<Password>YOURPASSWORD</Password>
</AccessRequest>
<?xml version="1.0" ?>
<TrackRequest>
<Request>
<TransactionReference>
<CustomerContext>guidlikesubstance</CustomerContext>
</TransactionReference>
<RequestAction>Track</RequestAction>
</Request>
<TrackingNumber>1Z9999999999999999</TrackingNumber>
</TrackRequest>
Run Code Online (Sandbox Code Playgroud)
我在使用C#中的1个XmlDocument创建此问题时遇到问题.当我尝试添加第二个时:
<?xml version="1.0" ?> or the <TrackRequest>
它会抛出一个错误:
System.InvalidOperationException:此文档已有"DocumentElement"节点.
我猜这是因为标准的XmlDocument只有1个根节点.有任何想法吗?
到目前为止,我的代码是:
XmlDocument xmlDoc = new XmlDocument();
XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
XmlElement rootNode = xmlDoc.CreateElement("AccessRequest");
rootNode.SetAttribute("xml:lang", "en-US");
xmlDoc.InsertBefore(xmlDeclaration, xmlDoc.DocumentElement);
xmlDoc.AppendChild(rootNode);
XmlElement licenseNode = xmlDoc.CreateElement("AccessLicenseNumber");
XmlElement userIDNode = xmlDoc.CreateElement("UserId");
XmlElement passwordNode = xmlDoc.CreateElement("Password");
XmlText licenseText = xmlDoc.CreateTextNode("mylicense");
XmlText userIDText = xmlDoc.CreateTextNode("myusername");
XmlText passwordText = xmlDoc.CreateTextNode("mypassword");
rootNode.AppendChild(licenseNode);
rootNode.AppendChild(userIDNode);
rootNode.AppendChild(passwordNode); …Run Code Online (Sandbox Code Playgroud) 我有包含CDATA的xml文件
我需要更新CDATA,就像在这个例子中一样.
我在这里修改"跨度"
<elements>
<![CDATA[-div[id|dir|class|align|style],-span[class|align]]]>
</elements>
Run Code Online (Sandbox Code Playgroud)
应该更新为
<elements>
<![CDATA[-div[id|dir|class|align|style],-span[class|align|style]]]>
</elements>
Run Code Online (Sandbox Code Playgroud)
我正在使用框架2.0 ..如何使用xmldocument这样做.
谢谢
我正在使用XmlDocument来处理xml
如何使用当前格式保存"XmlDocument"?
当前格式:
<?xml version="1.0" encoding="utf-8"?>
<root>
<element></element>
</root>
Run Code Online (Sandbox Code Playgroud)
码:
XmlDocument testDoc = new XmlDocument();
testDoc.Load(@"C:\Test.xml");
**(do reading/writing using only XmlDocument methods)**
testDoc.Save(@"C:\Test.xml");
Run Code Online (Sandbox Code Playgroud)
有一个类似的主题: XmlDocument类正在删除格式,c#,.NET
接受的答案是PreserveWhiteSpace = true,实际上删除了所有空格而不是保留它们.
例:
码:
XmlDocument testDoc = new XmlDocument();
testDoc.Load(@"C:\Test.xml");
testDoc.PreserveWhitespace = true;
testDoc.Save(@"C:\Test.xml");
Run Code Online (Sandbox Code Playgroud)
结果:
<?xml version="1.0" encoding="utf-8"?><root><element></element></root>
Run Code Online (Sandbox Code Playgroud) 我正在将一些代码从使用XmlDocument迁移到使用XDocument.作为此代码的一部分,我需要将一个元素从一个文档添加到另一个文档中.使用XmlDocument我可以使用ImportNode执行此操作,如何使用XDocument执行此操作?
我是xpath匹配的新手。在这里,我有一个将XML包含作为字符串传递的方法。我将其转换为XmlDocument。
public static void getProjectDataInfo(string content) {
XmlDocument doc = new XmlDocument();
doc.LoadXml(content);
}
Run Code Online (Sandbox Code Playgroud)
这是我的XML。它具有xmlns:my
<?xml version="1.0" encoding="UTF-8"?>
<my:myFields xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2012-02-03T16:54:46" xmlns:xd="http://schemas.microsoft.com/office/infopath/2003" xml:lang="en-us">
<my:Financial>
<my:Quote>
<my:CHARGE_TYPE>MRC</my:CHARGE_TYPE>
<my:Price>463.92</my:Price>
</my:Quote>
</my:Financial>
</my:myField>
Run Code Online (Sandbox Code Playgroud)
我只想获得
/my:myFields/my:Financial/my:Quote/my:Price
Run Code Online (Sandbox Code Playgroud)
但是我无法获取值,因此此XML具有xmlns。
请帮我。
我正在尝试使用HtmlAgilityPack更新外部Html.该属性显示为只读.我的问题是如何更新外部Html?注意:我确实需要更新外部html(不仅仅是内部html).这是代码:
// Check if there is a nested table
HtmlAgilityPack.HtmlNode nestedtable = tr.SelectSingleNode(".//table");
if (nestedtable != null)
{
// Save Inner/Outer Html and update Outer Html
string strInnerHtml = nestedtable.InnerHtml;
string strOuterHtml = nestedtable.OuterHtml;
string strNewOuterHtml = "<table><tr><td><table><tr><td>inner1update</td><td>inner2update</td></tr></table></td></tr></table>";
// Now update source HtmlDocument
nestedtable.OuterHtml = strNewOuterHtml;
// ^^^ Error line: Property or indexer
//HtmlAgilityPack.HtmlNode.OuterHtml' cannot be assigned to -- it is read only
}
Run Code Online (Sandbox Code Playgroud) 我是一个新手.XmlDocument我想在c#中创建嵌套的xml文档.通过一些reaserach i fount,XmlDocument如果size很小,建议创建xml的方法.
我在创建嵌套标签时遇到了一些麻烦
码:
XmlDocument doc = new XmlDocument();
XmlDeclaration xDeclare = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
XmlElement root = doc.DocumentElement;
doc.InsertBefore(xDeclare, root);
XmlElement el = (XmlElement)doc.AppendChild(doc.CreateElement("FIXML"));
el.AppendChild(doc.CreateElement("Header")).InnerText = "";
el.AppendChild(doc.CreateElement("RequestHeader")).InnerText = "";
el.AppendChild(doc.CreateElement("MessageKey")).InnerText = "";
el.AppendChild(doc.CreateElement("RequestUUID")).InnerText = "938692349";
Console.WriteLine(doc.OuterXml);
Run Code Online (Sandbox Code Playgroud)
它的输出为
<?xml version="1.0" encoding="UTF-8"?>
<FIXML>
<Header></Header>
<RequestHeader></RequestHeader>
<MessageKey></MessageKey>
<RequestUUID>938692349</RequestUUID>
</FIXML>
Run Code Online (Sandbox Code Playgroud)
但应该是这样的
<?xml version="1.0" encoding="UTF-8"?>
<FIXML>
<Header>
<RequestHeader>
<MessageKey>
<RequestUUID>938692349</RequestUUID>
</MessageKey>
</RequestHeader>
</Header>
</FIXML>
Run Code Online (Sandbox Code Playgroud) 我这样做:
var xml = new XmlDocument();
xml.AppendChild(xml.CreateXmlDeclaration("1.0", Encoding.UTF8.BodyName, "yes"));
var el = (XmlElement)xml.AppendChild(xml.CreateElement("Document"));
el.SetAttribute("xmlns", "urn:iso:std:iso:20022:tech:xsd:" + SepaSchemaUtils.SepaSchemaToString(schema));
Run Code Online (Sandbox Code Playgroud)
然后为了获得缩进的XML,我做了:
StringBuilder sb = new StringBuilder();
XmlWriterSettings settings = new XmlWriterSettings
{
Indent = true,
IndentChars = " ",
NewLineChars = "\r\n",
NewLineHandling = NewLineHandling.Replace
};
using (XmlWriter writer = XmlWriter.Create(sb, settings))
{
doc.Save(writer);
}
Run Code Online (Sandbox Code Playgroud)
我执行doc.Save(writer)时遇到异常.
System.Xml.XmlException:在同一个start元素标记内,前缀''不能从''重新定义为'urn:iso:std:iso:20022:tech:xsd:pain.001.001.03'.
我已经尝试过我能找到的一切.谢谢.
我想在Delphi Berlin 10.1中创建XML,我需要获取这样的文件:
<?xml version="1.0" encoding="UTF-8"?>
<p:FatturaElettronica versione="FPA12" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:p="http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2 http://www.fatturapa.gov.it/export/fatturazione/sdi/fatturapa/v1.2/Schema_del_file_xml_FatturaPA_versione_1.2.xsd">
<FatturaElettronicaHeader>
<DatiTrasmissione>
<IdTrasmittente>
<IdPaese>IT</IdPaese>
<IdCodice>01234567890</IdCodice>
</IdTrasmittente>
<ProgressivoInvio>00001</ProgressivoInvio>
<FormatoTrasmissione>FPA12</FormatoTrasmissione>
<CodiceDestinatario>AAAAAA</CodiceDestinatario>
</DatiTrasmissione>
...
Run Code Online (Sandbox Code Playgroud)
我写了这个程序
procedure Tfattura2_new_form.Button1Click(Sender: TObject);
Var
XML : IXMLDOCUMENT;
RootNode, CurNode, header[...]: IXMLNODE;
begin
XML := NewXMLDocument;
XML.Encoding := 'utf-8';
XML.Options := [doNodeAutoIndent];
RootNode := XML.AddChild('FatturaElettronica');
RootNode.Attributes['versione']:='FPA12';
RootNode.DeclareNamespace('ds','http://www.w3.org/2000/09/xmldsig#');
RootNode.DeclareNamespace('p','http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2');
RootNode.DeclareNamespace('xsi','http://www.w3.org/2001/XMLSchema-instance');
[...]
header := RootNode.AddChild('FatturaElettronicaHeader');
DatiTrasmissione := header.AddChild('DatiTrasmissione');
IdTrasmittente := DatiTrasmissione.AddChild('IdTrasmittente');
[...]
XMl.SaveToFile('C:\file.xml');
end;
Run Code Online (Sandbox Code Playgroud)
现在问题是我需要在根节点中有前缀p(p:FatturaElettronica ...)但如果我不知道如何:如果我设置
RootNode := XML.AddChild('p:FatturaElettronica');
Run Code Online (Sandbox Code Playgroud)
在xml文件中,我在每个标签中都有前缀p :.
<?xml version="1.0" encoding="utf-8"?>
<p:FatturaElettronica versione="FPA12" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:p="http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2" …Run Code Online (Sandbox Code Playgroud) xmldocument ×10
c# ×8
xml ×7
.net ×1
delphi ×1
doctype ×1
formatting ×1
linq-to-xml ×1
vb.net ×1
xpath ×1