由于这个错误,我试图SelectNode从XmlDocument课堂打电话和麻烦:
需要命名空间管理器或XsltContext.此查询具有前缀,变量或用户定义的函数.
我的代码:
public void Add(ref XmlDocument xmlFormat, String strName)
{
XmlDocument dom;
XSLTemplate xsl = null;
String strPath = "";
XmlNodeList nl;
XmlAttribute na;
int n;
nl = (XmlNodeList)xmlFormat.SelectNodes("//xsl:import/@href",nsm);
}
Run Code Online (Sandbox Code Playgroud)
和xsl:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:import href="stylesheets/r_adresetiket.xsl" />
<xsl:template match="/">
<xsl:call-template name="retouradres">
<xsl:with-param name="_retouradres" select="data/adresetiket/_retouradres" />
<xsl:with-param name="minofdir" select="data/adresetiket/afzendgegevens/afzendgegevens" />
<xsl:with-param name="checked" select="data/adresetiket/LB" />
</xsl:call-template>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud) 许多.NET函数使用XmlWriter输出/生成xml.输出到文件/字符串/内存是一个非常的操作:
XmlWriter xw = XmlWriter.Create(PutYourStreamFileWriterEtcHere);
xw.WriteStartElement("root");
...
Run Code Online (Sandbox Code Playgroud)
有时,您需要操作生成的Xml,因此需要将其加载到XmlDocument中,或者由于某些其他原因可能需要XmlDocument,但您必须使用XmlWriter生成XML.例如,如果您调用第三方库中的函数,该函数仅输出到XmlWriter.
您可以做的一件事是将xml写入字符串,然后将其加载到XmlDocument中:
StringWriter S = new StringWriter();
XmlWriter xw = XmlWriter.Create(S);
/* write away */
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(S.ToString());
Run Code Online (Sandbox Code Playgroud)
但是这效率很低 - 首先将所有xml信息序列化为字符串,然后再次解析字符串以创建DOM.
如何指出XmlWriter直接构建XmlDocument?
以下代码应该找到相应的项目标记并将其从XmlDocument中删除,但是当我测试它时,它会说:
要删除的节点不是此节点的子节点.
有谁知道这样做的正确方法?
public void DeleteProject (string projectName)
{
string ccConfigPath = ConfigurationManager.AppSettings["ConfigPath"];
XmlDocument configDoc = new XmlDocument();
configDoc.Load(ccConfigPath);
XmlNodeList projectNodes = configDoc.GetElementsByTagName("project");
for (int i = 0; i < projectNodes.Count; i++)
{
if (projectNodes[i].Attributes["name"] != null)
{
if (projectName == projectNodes[i].Attributes["name"].InnerText)
{
configDoc.RemoveChild(projectNodes[i]);
configDoc.Save(ccConfigPath);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
UPDATE
固定.我做了两件事:
XmlNode project = configDoc.SelectSingleNode("//project[@name='" + projectName + "']");
Run Code Online (Sandbox Code Playgroud)
用XPath查询替换了For循环,这不是为了修复它,只是因为它是一种更好的方法.
实际修复是:
project.ParentNode.RemoveChild(project);
Run Code Online (Sandbox Code Playgroud)
感谢Pat和Chuck的建议.
我正在将字符串加载到包含以下结构的XML文档:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Compile Include="clsWorker.cs" />
</ItemGroup>
</Project>
Run Code Online (Sandbox Code Playgroud)
然后我将所有加载到xmldocument:
XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml(Xml);
Run Code Online (Sandbox Code Playgroud)
然后出现以下问题:
XmlNode Node = xmldoc.SelectSingleNode("//Compile"); // return null
Run Code Online (Sandbox Code Playgroud)
当我从根元素(Project)中删除xmlns属性时,它的工作正常,如何改进我的SelectSingleNode以返回相关元素?
我已经尝试过但未能找到如何从GET返回的XMLDocument中获取整个XML字符串.关于如何查找或替换对象中的特定元素,有很多关于SO的问题,但我似乎无法找到如何将整个文档作为字符串获得任何答案.
我正在使用的例子来自这里."做xml的事情" - 部分是我现在所处的位置.我觉得这应该是非常微不足道的,但我没有弄清楚如何.是否有可用于此目的的"xml.data()"或类似内容?
$.ajax({
url: 'document.xml',
type: 'GET',
dataType: 'xml',
timeout: 1000,
error: function(){
alert('Error loading XML document');
},
success: function(xml){
// do something with xml
}
});
Run Code Online (Sandbox Code Playgroud)
用例是我想将xml提供给flash插件,为此我需要将实际的XML作为字符串.
我已经实现了在应用程序初始化时使用XmlTextWriter创建下面的XML文件.
并且知道我不知道如何使用XmlDocument和XmlNode更新childNode id值.
是否有一些属性来更新id值?我试过InnerText但是失败了.谢谢.
<?xml version="1.0" encoding="UTF-8"?>
<Equipment xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<License licenseId="" licensePath=""/>
<DataCollections>
<GroupAIDs>
<AID id="100">
<Variable id="200"/>
<Variable id="201"/>
</AID>
<AID id="">
<Variable id="205"/>
</AID>
<AID id="102"/>
</GroupAIDs>
<GroupBIDs>
<BID id="2000">
<AID id="100"/>
</BID>
<BID id="2001">
<AID id="101"/>
<AID id="102"/>
</BID>
</GroupBIDs>
<GroupCIDs>
<BID id="8"/>
<BID id="9"/>
<BID id="10"/>
</GroupCIDs>
</DataCollections>
</Equipment>
Run Code Online (Sandbox Code Playgroud) 我有以下(作为示例)XML文件和XSD.
<?xml version="1.0" encoding="utf-8" ?>
<foo>
<DateVal>2010-02-18T01:02:03</DateVal>
<TimeVal>PT10H5M3S</TimeVal>
</foo>
Run Code Online (Sandbox Code Playgroud)
和
version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="foo">
<xs:complexType>
<xs:sequence>
<xs:element name="DateVal" type="xs:dateTime" />
<xs:element name="TimeVal" type="xs:duration" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Run Code Online (Sandbox Code Playgroud)
然后是以下C#代码:
static void Main(string[] args)
{
XmlDocument xd = new XmlDocument();
XmlSchema xs;
using (var fs = File.OpenRead(FilePath + "SimpleFields.xsd"))
{
xs = XmlSchema.Read(fs, null);
}
xd.Schemas.Add(xs);
xd.Load((FilePath + "SimpleFields.xml"));
xd.Validate(null);
var el_root = xd.DocumentElement;
var el_date = (XmlElement)el_root.SelectSingleNode("./DateVal");
//WANTED: el_date.Value = 2010-02-18 01:02:03 (as a DateTime Object) …Run Code Online (Sandbox Code Playgroud) 说我有这个构造函数:
/// <summary>
/// Example comment.
/// </summary>
public SftpConnection(string host, string username,
string password, int port) {...}
Run Code Online (Sandbox Code Playgroud)
有这些重载:
public SftpConnection(string host, string username, string password)
: this(host, username, password, 22) { }
public SftpConnection(string host, string username, int port)
: this(host, username, "", port) { }
public SftpConnection(string host, string username)
: this(host, username, "", 22) { }
Run Code Online (Sandbox Code Playgroud)
而在现实中,XML注释是相当大的,有param,example和exception元素等等.
有没有办法在重载中添加一个特殊的XML注释单行,这样他们就可以使用完全相同的注释,这样我就不需要复制粘贴整个巨大的原始注释了?
我想的是:<use cref="SftpConnection(string,string,string,int)" />当然不起作用.
我知道这个include元素,但我得到的印象是它从XML文件读取注释,我不想要 - 我希望注释仍然在代码中可见,但只有一次.
谢谢 …
我在浏览XML文档(使用C#)时遇到问题,并获得所有必要的值.我成功浏览了XML文档中所有指定的XmlNodeLists,成功获取了所有XmlNode值,但我必须在此XmlNodeList之外获取一些值.
例如:
<?xml version="1.0" encoding="UTF-8" ?>
<Element xsi:schemaLocation="http://localhost/AML/CaseInvestigationMangement/Moduli/XmlImportControls/xsdBorrow.xsd xsd2009027_kor21.xsd" Kod="370" xmlns="http://localhost/AML/CaseInvestigationMangement/Moduli/XmlImportControls/xsdBorrow.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
/2001/XMLSchema-instance">
<ANode>
<BNode>
<CNode>
<Example>
<Name>John</Name>
<NO>001</NO>
</Example>
</CNode>
</BNode>
<ID>1234</ID>
<Date>2011-10-01</Date>
</ANode>
<ANode>
<BNode>
<CNode>
<Example>
<Name>Mike</Name>
<NO>002</NO>
</Example>
</CNode>
</BNode>
<ID>5678</ID>
<Date>2011-03-31</Date>
</ANode>
</Element>
Run Code Online (Sandbox Code Playgroud)
这是获取XML文档中每个找到的ANode中节点Name和NO的值的代码:
XmlDocument xml = new XmlDocument();
xml.LoadXml(myXmlString); //myXmlString is the xml file in string //copying xml to string: string myXmlString = xmldoc.OuterXml.ToString();
XmlNodeList xnList = xml.SelectNodes("/Element[@*]/ANode/BNode/CNode");
foreach (XmlNode xn in xnList)
{
XmlNode example = xn.SelectSingleNode("Example");
if (example != null)
{
string …Run Code Online (Sandbox Code Playgroud) 我正在加载很多xml文档,其中一些返回错误,如"十六进制值0x12,是一个无效字符",并且有不同的字符.如何删除它们?
xmldocument ×10
c# ×8
xml ×8
.net ×2
html ×1
javascript ×1
jquery ×1
overloading ×1
regex ×1
replace ×1
selectnodes ×1
xmlnode ×1
xmlwriter ×1
xsd ×1