通過寫這個
Dim AiD As XmlElement = myXML.CreateElement("ns8:AiD", aNamespace)
AiD.InnerText = "myInnerText"
AiD.SetAttribute("xsi:type", "ns17:anObject")
AiD.SetAttribute("xmlns:ns17", "http://aLink")
AiD.SetAttribute("xmlns:xsi", "http://anotherLink")
WR.AppendChild(AiD)
我希望我的 XML 檔案包含以下內容:
<ns8:AiD xsi:type="ns17:anObject" xmlns:ns17=http://aLink xmlns:xsi=http://anotherLink>myInnerText</ns8:AiD>
不幸的是,它包含以下內容:
<ns8:AiD type="ns17:anObject" xmlns:ns17="http://aLink" xmlns:xsi="http://anotherLink">myInnerText</ns8:AiD>
因此包含引號并且缺少“xsi:”。我已經讀過引號還不錯。但是為什么'xsi:'不見了?
這些函式是常用的 .NET System.Xml 函式。MyXML是一個 System.XML.XmlDocument。
uj5u.com熱心網友回復:
以防萬一,這是您需要的 c# 實作。
請檢查正確的 XML 輸出。所有命名空間都在 XML 本身中顯式宣告,否則無法使用。
這是 VB.NET 的做法:How to create a document with namespaces in Visual Basic (LINQ to XML)
C#
void Main()
{
XNamespace ns8 = "http://www.missingNamespace.com";
XNamespace xsi = "http://anotherLink";
XNamespace ns17 = "http://aLink";
XElement AiD = new XElement(ns8 "AiD",
new XAttribute(XNamespace.Xmlns "ns8", ns8.NamespaceName),
new XAttribute(XNamespace.Xmlns "ns17", ns17.NamespaceName),
new XAttribute(XNamespace.Xmlns "xsi", xsi.NamespaceName),
new XAttribute(xsi "type", "ns17:anObject"),
"myInnerText");
Console.WriteLine(AiD );
}
輸出
<ns8:AiD xmlns:ns8="http://www.missingNamespace.com" xmlns:ns17="http://aLink" xmlns:xsi="http://anotherLink" xsi:type="ns17:anObject">myInnerText</ns8:AiD>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/511594.html
標籤:xmlVB.net
