如何将新的子节点添加到现有的子节点
我需要让汽车成为赛道的子节点。我在网上查了一下,并尝试了几种不同的方法,但无法使其正常工作。
这是我生成的 xml 文件。
<RTDX msgid="3642728b-b75c-4196-9fad-5c4e882c3a9d" msgtime="0001-01-01 00:00:00.000" xmlns="http://www.aps-technology.com">
<EventTime>2012-02-09 19:38:13.802</EventTime>
<Track name="P7E Switch" />
<Car name="CSXT8423" sequence="4" orientation="A" axles="1" />
</RTDX>
但这是预期的格式。
<RTDX msgid="3642728b-b75c-4196-9fad-5c4e882c3a9d" msgtime="0001-01-01 00:00:00.000" xmlns="http://www.aps-technology.com">
<EventTime>2011-12-07 05:00:03.579</EventTime>
<Track name="P7E Switch">
<Car name="CSXT8423" sequence="1" orientation="A"/>
</Track>
</RTDX>
这是我生成 Xml 文件的方法。请注意,消息的第一行是在其他地方生成的,我无法更改它。
protected override void SaveApsXml(System.Xml.XmlNode node)
{
base.SaveApsXml(node);
node.AppendNewChild("EventTime").SetElementText(this.EventTime.ToString(ApsMessage.DateTimeFormat));
node.AppendNewChild("Track").SetAttribute("name", this.Track); //Pretty sure this is whats causing the problem
this.SequenceCar.SaveApsXml(node.AppendNewChild("Car"));
}
作为参考,这是 AppendNewChild 方法
public static XmlNode AppendNewChild(this XmlNode node, string name)
{
XmlNode child = node.OwnerDocument.CreateElement(name, node.NamespaceURI);
return node.AppendChild(child);
}
I need to make a car a child node of a track. I have looked online, and have tried several different ways, but I can't get it to work properly.
Here is my xml file I generate.
<RTDX msgid="3642728b-b75c-4196-9fad-5c4e882c3a9d" msgtime="0001-01-01 00:00:00.000" xmlns="http://www.aps-technology.com">
<EventTime>2012-02-09 19:38:13.802</EventTime>
<Track name="P7E Switch" />
<Car name="CSXT8423" sequence="4" orientation="A" axles="1" />
</RTDX>
however this is the expected format.
<RTDX msgid="3642728b-b75c-4196-9fad-5c4e882c3a9d" msgtime="0001-01-01 00:00:00.000" xmlns="http://www.aps-technology.com">
<EventTime>2011-12-07 05:00:03.579</EventTime>
<Track name="P7E Switch">
<Car name="CSXT8423" sequence="1" orientation="A"/>
</Track>
</RTDX>
This is my method that generates the Xml File. Note that the first line of the message is generated elsewhere and I cannot change that.
protected override void SaveApsXml(System.Xml.XmlNode node)
{
base.SaveApsXml(node);
node.AppendNewChild("EventTime").SetElementText(this.EventTime.ToString(ApsMessage.DateTimeFormat));
node.AppendNewChild("Track").SetAttribute("name", this.Track); //Pretty sure this is whats causing the problem
this.SequenceCar.SaveApsXml(node.AppendNewChild("Car"));
}
For reference this is the AppendNewChild Method
public static XmlNode AppendNewChild(this XmlNode node, string name)
{
XmlNode child = node.OwnerDocument.CreateElement(name, node.NamespaceURI);
return node.AppendChild(child);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可能是因为您将汽车附加到了错误的节点。
另请注意,您预期的格式示例是不正确的 xml(根元素名称不匹配 - RTDX 与 RTDT)。
It's likely because you're appending the car to the wrong node.
Also note that your expected format-example is incorrect xml (root element name doesn't match - RTDX vs RTDT).