【发布时间】:2014-06-05 07:31:08
【问题描述】:
我正在通过 C# 代码动态构建 xml,但出现此错误
"指定的节点不能作为该节点的有效子节点插入 节点,因为指定的节点是错误的”
这是我的主要课程
internal class Program
{
private static void Main(string[] args)
{
CreateXml xml = new CreateXml();
xml.multipleXML();
}
}
我在下面出现运行时异常的地方注释为“错误”。请帮助如何解决此错误。
我的 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;
}
}
【问题讨论】:
-
har07 请在下面找到引发错误的行。 countNode.AppendChild(SetNewLocation("Bangalore","30","30"));//错误
-
这是因为
SetNewLocation()方法在预期类型为XmlElement时返回XmlDocument。但我不知道如何解决它,除非你能解释该方法应该做什么。 -
@har07 但该方法的返回类型是 XMLNode,当我最终使用 xmlNode AppendChild 时,它不应该给我任何错误。让我知道我是否错了?...此方法必须构建一个像`
Bangalore 1 CASES 1 SINGLES -
XmlDocument 和 XmlElement 都是 XmlNode 的后代。您可以通过将这部分:
docCountXml.SelectSingleNode("/");更改为:docCountXml.DocumentElement;来避免该错误,但随后您会发现另一个问题:您无法附加从不同 XmlDocument 实例创建的 XmlNode。。跨度>
标签: c# asp.net xml asp.net-mvc-3 asp.net-mvc-4