簡體   English   中英

將名稱空間添加到XML根VB.net時出現問題

[英]Problem adding namespace to XML root VB.net

我在將namepeace添加到XML的根元素時遇到一些問題。 這就是我需要的:

<?xml version="1.0" encoding="utf-8"?>
<T:Test xsi:schemaLocation="http://www.w3.org/2001/XMLSchema 
test.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:T="http://test.bfa">

這就是我所擁有的:

<?xml version="1.0" encoding="utf-8"?>
<Test xsi:schemaLocation="http://www.w3.org/2001/XMLSchema Test.xsd" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

我的代碼:

Dim xmldecl As XmlDeclaration
xmldecl = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", "")

Dim root As XmlElement = xmlDoc.DocumentElement
xmlDoc.InsertBefore(xmldecl, root)
Dim schemaLocation As XmlAttribute = xmlDoc.CreateAttribute("xsi","schemaLocation", "http://www.w3.org/2001/XMLSchema-instance")
schemaLocation.Value = "http://www.w3.org/2001/XMLSchema test.xsd"

Dim xmlTestNode = xmlDoc.CreateElement(TEST_NODO)
xmlTestNode.Attributes.Append(schemaLocation)

嘗試這個

    Dim rxe As XElement

    rxe = <T:Test xsi:schemaLocation="http://www.w3.org/2001/XMLSchematest.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xmlns:T="http://test.bfa"></T:Test>

    rxe.Save("your path here")

保存后,它將具有正確的聲明,

編輯:

    Dim rxe As XElement

    rxe = <T:Test xsi:schemaLocation="http://www.w3.org/2001/XMLSchematest.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xmlns:T="http://test.bfa"></T:Test>

    Dim oldXE As XElement
    oldXE = XElement.Load("path to old here")

    rxe.Add(oldXE.Elements)

    rxe.Save("your path here")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM