我想让所有的孩子从给定节点到叶子而不使用递归.那可能吗?我知道如何在LINQ to XML中执行此操作,但是在XmlNode:S中存在一些问题
这个问题可能看起来很愚蠢,但我无法找到很多帮助,如何使用PHP找到最后一个孩子的节点值,即使它与JS一块蛋糕.这就是我的XML目前的样子:
<?xml version="1.0"?>
<files>
<file>.DS_Store</file>
<file>ID2PDF_log_1.xml</file>
<file>ID2PDF_log_12.xml</file>
<file>ID2PDF_log_15.xml</file>
</files>
Run Code Online (Sandbox Code Playgroud)
这是php代码:
$filename = 'files.xml'; //my xml file name
$dom = new DomDocument();
$dom->load($filename);
$elements = $dom->getElementsByTagName('file');
echo $elements->lastChild(); // This is obviously not working
/*I get an error that I am trying to access an undefined method in DOMNodeList. Now, I know
that lastChild is a property of DOMNode. But I can't figure out how I can change my code to
get this to work.*/
Run Code Online (Sandbox Code Playgroud)
我想回应一下
ID2PDF_log_15.xml
Run Code Online (Sandbox Code Playgroud)
谁能告诉我如何完成这项工作?
PS:我不想更改xml文件结构,因为我是通过脚本创建的,而且我是一个懒惰的程序员.但是,我确实做了我的研究才得到这个.没有帮助.
我确实尝试获取节点"文件"中的元素数量,然后使用项目(#),但这似乎也不起作用.
谢谢 …
如何获取当前节点的XPath XMLReader?
例如:
<Employee>
<Entity>
<Id>1</Id>
</Entity>
</Employee>
Run Code Online (Sandbox Code Playgroud)
所以,我需要得到的XPath 1是Employee/Entity/Id.有任何想法吗?
using (var reader = XmlReader.Create(basePath, settings))
{
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Text)
{
// need to get xpath of the text node
}
else if (reader.NodeType == XmlNodeType.Element)
{
// need to get xpath of the current node
}
}
}
Run Code Online (Sandbox Code Playgroud) 我似乎总是在C#中将数据转换为XML或从XML转换数据时遇到问题.它总是希望您创建一个完整的XMLDocument对象,即使您认为不应该这样做.在这种情况下,我在MS SQL 2005服务器中有一个SQLXML列,我试图将其拉出并推入需要XMLNode作为参数的函数.您会认为这很容易,但除了将其转换为字符串并创建新的XMLNode对象之外,我无法找到正确的方法.
我可以使用SqlDataReader,sqlComm.ExecuteReader()来加载读取器,使用sqlReader.GetSqlXml(0)来获取SQLXML对象,但是如何将其转换为XmlNode?
相反,我可以使用sqlComm.ExecuteXmlReader()来获取XmlReader,但是如何从阅读器中提取XmlNode?http://bytes.com/forum/thread177004.html表示无法使用XmlTextReader,我应该使用XmlNodeReader吗?
请帮忙!
我有一个节点,这个节点包含5个子节点.其中三个是RatePlan.如何使用LINQ选择那些RatePlan子节点?
让我们澄清一下:
我的xml是这样的:
<hotels>
<hotel id="1" name="hotel 1">
<telephone>123456789</telephone>
<fax>123</fax>
<address>hotels address</address>
<hotelRatePlan>10</hotelRatePlan>
<hotelRatePlan>11</hotelRatePlan>
<hotelRatePlan>12</hotelRatePlan>
</hotel>
<hotel id="2" name="hotel 2">
<telephone>123456789</telephone>
<fax>123</fax>
<address>hotels address</address>
<hotelRatePlan>100</hotelRatePlan>
<hotelRatePlan>110</hotelRatePlan>
<hotelRatePlan>120</hotelRatePlan>
</hotel>
<hotel id="3" name="hotel 3">
<telephone>123456789</telephone>
<fax>123</fax>
<address>hotels address</address>
<hotelRatePlan>10</hotelRatePlan>
<hotelRatePlan>11</hotelRatePlan>
<hotelRatePlan>12</hotelRatePlan>
</hotel>
</hotels>
Run Code Online (Sandbox Code Playgroud)
我正在使用XMLDocument来读取XML文件.在我阅读之后,我使用SelectNodes进行选择.当我得到第一个酒店信息时,我想选择特定的儿童节点(hotelRatePlan).我怎样才能做到这一点?
以下代码采用XmlNode数据类型并DataSet使用XmlNode内容填充对象.然后我将数据集的内容写入文件.
public void PopulateDataSet(XmlNode node)
{
XmlNodeReader reader = new XmlNodeReader(node);
DataSet ds = new DataSet();
ds.ReadXml(reader);
system.Guid guid = System.Guid.NewGuid();
string name = string.Format("{0}{1}_{2}.xml", Utility.XmlOutputPath, Utility.XmlOutputFileName, guid.ToString());
//need to write "Node empty" to file if XmlNode object is empty of null
ds.WriteXml(name, XmlWriteMode.IgnoreSchema);
}
Run Code Online (Sandbox Code Playgroud)
问题是我遇到了一个不会将内容写入文件的场景.如何确定XmlNode对象是空还是空?
这个问题是对已回答问题的跟进: XmlDocument.SelectSingleNode和前缀+ xmlNamespace问题
问题是,将来可能会在没有警告的情况下更改收到的xml的名称空间前缀,因此我们想知道是否有任何方法可以使用SelectSingleNode但省略元素的前缀.
(我们知道我们可以删除传入xml的所有前缀,但它需要更多步骤....虽然如果提供代码我们会认为它是一个有效的答案......)
所以,这可能是一个非常愚蠢的问题,但我很难找到真正有用的答案.
所以我得到了一个看起来像这样的xml.
<LocationList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://xmlopen.rejseplanen.dk/xml/rest/hafasRestStopsNearby.xsd">
<StopLocation name="Tesdorpfsvej (Odense Kommune)" x="10400699" y="55388303" id="461769300" distance="205"/>
<StopLocation name="Tesdorpfsvej / Munkebjergvej (Odense Kommune)" x="10400681" y="55388303" id="461055900" distance="206"/>
<StopLocation name="Munkebjerg Plads (Odense Kommune)" x="10401589" y="55386873" id="461120400" distance="312"/>
<StopLocation name="Munkebjergvej (Odense Kommune)" x="10397463" y="55390514" id="461038500" distance="372"/>
<StopLocation name="Jagtvej (Odense Kommune)" x="10396456" y="55389489" id="461019400" distance="420"/>
<StopLocation name="Allegade (Odense Kommune)" x="10396618" y="55390721" id="461026900" distance="430"/>
<StopLocation name="Guldbergsvej (Odense Kommune)" x="10396923" y="55391422" id="461104100" distance="443"/>
<StopLocation name="Nansensgade (Odense Kommune)" x="10409436" y="55391799" id="461115400" distance="472"/>
<StopLocation name="Chr. Sonnes Vej (Odense Kommune)" x="10404483" y="55385219" …Run Code Online (Sandbox Code Playgroud) 我用Java编程,我正在使用DOM解析器.
我注意到之间存在差异
org.w3c.dom.Node
Run Code Online (Sandbox Code Playgroud)
和
Node
Run Code Online (Sandbox Code Playgroud)
但我不知道那是什么区别.
当我试图调用一个类型为:org.w3c.dom.Node的参数的方法时,我想出来了 .
我输入了一个Node参数,我发现了以下错误:
Exception in thread "main" java.lang.ClassCastException:
com.sun.org.apache.xerces.internal.dom.DeferredTextImpl cannot be cast to javax.xml.soap.Node
at elementchildren.ElementChildren.main(ElementChildren.java:50)
Java Result: 1
Run Code Online (Sandbox Code Playgroud)
你能帮我吗?
我正在尝试制作一个 XML 节点及其所有子节点的副本,但使用不同的 XML 父节点名称,但给我一个错误,这是 xml 文件:
<Servers>
<MyServer>
<Host>0.0.0.0</Host>
<Port>12</Port>
<User>USER</User>
</MyServer>
</Servers>
Run Code Online (Sandbox Code Playgroud)
我正在尝试做的是 MyServer 的副本,其中包含所有子节点和值,但名称不同......像这样
<Servers>
<MyServer>
<Host>0.0.0.0</Host>
<Port>12</Port>
<User>USER</User>
</MyServer>
<MyCopyofMyServer>
<Host>0.0.0.0</Host>
<Port>12</Port>
<User>USER</User>
</MyCopyofMyServer>
</Servers>
Run Code Online (Sandbox Code Playgroud)
我所做的是这样的:
public void CopyInterface(string NewServer, string ServerToCopy)
{
xmldoc.Load(XMLInterfacesFile);
XmlNode NodeToCopy = xmldoc.SelectSingleNode("Servers/" + ServerToCopy);
XmlNode deep = NodeToCopy.CloneNode(true);
deep.InnerXml = deep.InnerXml.Replace(ServerToCopy, NewServer);
xmldoc.AppendChild(deep); //Throwing an exception here!
xmldoc.Save(XMLInterfacesFile);
}
Run Code Online (Sandbox Code Playgroud)
例外:此文档已有一个“DocumentElement”节点。
任何的想法?
xmlnode ×10
c# ×7
xml ×4
.net ×1
copy ×1
dataset ×1
java ×1
linq ×1
linq-to-xml ×1
null ×1
php ×1
selectnodes ×1
sqlxml ×1
xml-parsing ×1
xmlreader ×1