c#-无法将指定的节点作为该节点的有效子节点插入,因为指定的节点类型错误
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c#-无法将指定的节点作为该节点的有效子节点插入,因为指定的节点类型错误,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4669字,纯文字阅读大概需要7分钟。
内容图文
![c#-无法将指定的节点作为该节点的有效子节点插入,因为指定的节点类型错误](/upload/InfoBanner/zyjiaocheng/679/7e043c4181e2479598c4d18c44e29241.jpg)
我正在通过C#代码动态构建xml,但出现此错误
“The specified node cannot be inserted as the valid child of this
node, because the specified node is the wrong”
这是我的主班
internal class Program
{
private static void Main(string[] args)
{
CreateXml xml = new CreateXml();
xml.multipleXML();
}
}
我在下面的运行时异常注释为“ ERROR”.请帮助解决该错误.
我的Xml类在这里.
internal class CreateXml
{
private XmlDocument HandlingXmlDoc;
private String baseHandlingXML = "<?xml version=\"1.0\" encoding=\"utf-8\"?><SHandling><BLocation><SLocation><Identifier>02898</Identifier></SLocation></BLocation><Context><UserName>0289800001</UserName><Application>STOCK</Application></Context><Counting><SubmissionDateTime>2014-04-02T16:38:48.9345238+01:00</SubmissionDateTime><ProcessImmediately>YES</ProcessImmediately><Counts><Count><ProductIdentifier>050025488</ProductIdentifier><CountDateTime>2014-04-02T16:38:49.366567+01:00</CountDateTime><LocationCounts></LocationCounts></Count></Counts></Counting></SHandling>";
private XmlDocument locCountXmlDocument;
private String baseLocCountXML = "<LocationCount><Name>Bangalore</Name><SCounts><SCount><Quantity>1</Quantity><UnitOfMeasure>CASES</UnitOfMeasure></SCount><SCount><Quantity>1</Quantity><UnitOfMeasure>SINGLES</UnitOfMeasure></SCount></SCounts></LocationCount>";
public CreateXml()
{
Initialise();
}
public String GetStockHandlingXmlString { get { return HandlingXmlDoc.OuterXml; } }
public XmlDocument GetStockHandlingXmlDocument { get { return HandlingXmlDoc; } }
private void Initialise()
{
HandlingXmlDoc = new XmlDocument();
HandlingXmlDoc.LoadXml(baseHandlingXML);
locCountXmlDocument = new XmlDocument();
locCountXmlDocument.LoadXml(baseLocCountXML);
}
public void multipleXML()
{
XmlNode countNode = HandlingXmlDoc.CreateNode(XmlNodeType.Element, "Count", null);
XmlNode productIdentifierNode = HandlingXmlDoc.CreateNode(XmlNodeType.Element, "ProductIdentifier", null);
productIdentifierNode.InnerText = "123345";
XmlNode countDateTimeNode = HandlingXmlDoc.CreateNode(XmlNodeType.Element, "CountDateTime", null);
countDateTimeNode.InnerText = DateTime.Now.ToString();
//XmlNode locationCountNode = HandlingXmlDoc.CreateNode(XmlNodeType.Element, "LocationCounts", null);
countNode.AppendChild(productIdentifierNode);
countNode.AppendChild(countDateTimeNode);
countNode.AppendChild(SetNewLocation("Bangalore","30","30"));//ERROR
HandlingXmlDoc.SelectSingleNode("//SHandling//Counting//Counts").AppendChild(countNode);
}
private XmlNode SetNewLocation(String location, String casesQuantity, String singlesQuantity)
{
XmlDocument docCountXml = new XmlDocument();
docCountXml.LoadXml(baseLocCountXML);
SetValue(docCountXml, "LocationCount/Name", location);
var xmlNodeList = docCountXml.SelectNodes("LocationCount/SCounts/SCount/Quantity");
xmlNodeList[0].FirstChild.Value = casesQuantity;
xmlNodeList[1].FirstChild.Value = singlesQuantity;
return docCountXml.SelectSingleNode("/");
}
private static void SetValue(XmlDocument xmlDocument, String key, String value)
{
var xmlNode = xmlDocument.SelectSingleNode(key);
xmlNode.FirstChild.Value = value;
}
}
解决方法:
问题是您要从其他xml插入外部xmlnode.
我的编辑在这里,我使用一种解决方法将xmlnode加载到新文档中,然后将其附加到您的节点
countNode.AppendChild(countNode.OwnerDocument.ImportNode(test, true));
试试这个解决方案
public void multipleXML()
{
XmlNode countNode = HandlingXmlDoc.CreateNode(XmlNodeType.Element, "Count", null);
XmlNode productIdentifierNode = HandlingXmlDoc.CreateNode(XmlNodeType.Element, "ProductIdentifier", null);
productIdentifierNode.InnerText = "123345";
XmlNode countDateTimeNode = HandlingXmlDoc.CreateNode(XmlNodeType.Element, "CountDateTime", null);
countDateTimeNode.InnerText = DateTime.Now.ToString();
//XmlNode locationCountNode = HandlingXmlDoc.CreateNode(XmlNodeType.Element, "LocationCounts", null);
countNode.AppendChild(productIdentifierNode);
countNode.AppendChild(countDateTimeNode);
var test = SetNewLocation("Bangalore", "30", "30");
countNode.AppendChild(countNode.OwnerDocument.ImportNode(test, true));
//countNode.AppendChild(test);//ERROR
HandlingXmlDoc.SelectSingleNode("//SHandling//Counting//Counts").AppendChild(countNode);
}
和SetNewLocation函数中的另一个编辑
(我的编辑返回docCountXml.SelectSingleNode(“ LocationCount”);)
private XmlNode SetNewLocation(String location, String casesQuantity, String singlesQuantity)
{
XmlDocument docCountXml = new XmlDocument();
docCountXml.LoadXml(baseLocCountXML);
SetValue(docCountXml, "LocationCount/Name", location);
var xmlNodeList = docCountXml.SelectNodes("LocationCount/SCounts/SCount/Quantity");
xmlNodeList[0].FirstChild.Value = casesQuantity;
xmlNodeList[1].FirstChild.Value = singlesQuantity;
return docCountXml.SelectSingleNode("LocationCount");
}
内容总结
以上是互联网集市为您收集整理的c#-无法将指定的节点作为该节点的有效子节点插入,因为指定的节点类型错误全部内容,希望文章能够帮你解决c#-无法将指定的节点作为该节点的有效子节点插入,因为指定的节点类型错误所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。