[英]creating elements with namespace prefix based on multipart xml root declaration
If I do something like this: 如果我做这样的事情:
System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
XmlElement e = xmlDoc.CreateElement("ShipmentReceiptNotification");
e.SetAttribute("xmlns", "urn:rosettanet:specification:interchange:ShipmentReceiptNotification:xsd:schema:02.02");
e.SetAttribute("xmlns:ssdh", "urn:rosettanet:specification:domain:Procurement:AccountClassification:xsd:codelist:01.03");
XmlNode ShipmentReceiptNotification0Node = e;
ShipmentReceiptNotification0Node.InnerText = String.Empty;
xmlDoc.AppendChild(ShipmentReceiptNotification0Node);
XmlNode DocumentHeader1Node = xmlDoc.CreateElement("ssdh:DocumentHeader");
ShipmentReceiptNotification0Node.AppendChild(DocumentHeader1Node);
It will result in the prefix of the second node ssdh
not to display, only DocumentHeader
is displayed. 这将导致第二个节点ssdh
的前缀不显示,仅显示DocumentHeader
。 How could I fix this? 我该如何解决?
You need to create it like this: 您需要这样创建它:
XmlNode DocumentHeader1Node = xmlDoc.CreateElement("ssdh", "DocumentHeader", "urn:rosettanet:specification:domain:Procurement:AccountClassification:xsd:codelist:01.03");
The point is XmlDocument
needs to know which namespace prefix (first argument) corresponds which namespace URI (third argument). 关键是XmlDocument
需要知道哪个名称空间前缀(第一个参数)对应于哪个名称空间URI(第三个参数)。 A little bit counter-intuitive, but this is the way it works. 有点违反直觉,但这就是它的工作方式。
Also note that the line ShipmentReceiptNotification0Node.InnerText = String.Empty;
另请注意,该行ShipmentReceiptNotification0Node.InnerText = String.Empty;
is useless; 没有用 it's safe to omit it, the element is empty by default . 可以忽略它, 默认情况下该元素为空。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.