指定的节点不能作为该节点的有效子节点插入,因为指定的节点类型错误

本文关键字:节点 因为 错误 插入 类型 不能 有效 子节点 | 更新日期: 2023-09-27 18:06:44

我通过c#代码动态构建xml,我得到这个错误

"指定的节点不能作为此节点的有效子节点插入节点,因为指定的节点是错误的"

这是我的主类

internal class  Program
    {
        private static void Main(string[] args)
        {
            CreateXml xml = new CreateXml();
            xml.multipleXML();
        }
    }

我已经评论为"错误"下面,我得到运行时异常。请帮忙解决这个错误。

My Xml Class is here.

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函数中的另一个编辑

(my edit return 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");
        }

表示此处和此处的引用