2015-07-20 21 views
0

將字符串到XML節點我喜歡這個在VB.NET

<trueFalseQuestion id="585" status="correct" maxPoints="10" 
        maxAttempts="1" 
        awardedPoints="10" 
        usedAttempts="1" 
        xmlns="http://www.ispringsolutions.com/ispring/quizbuilder/quizresults"> 
    <direction>You have NO control over how you treat customers.</direction> 
    <answers correctAnswerIndex="1" userAnswerIndex="1"> 
     <answer>True</answer> 
     <answer>False</answer> 
    </answers> 
</trueFalseQuestion> 

數據庫列過一個XML字符串,但我需要做這個字符串XML操作,如選擇其名稱,屬性值,內文等我怎樣才能使這成爲可能,從這個字符串

編輯

林共享的代碼片段我試過,但沒有工作

 Dim myXML As String 
     Dim gDt As New DataTable 
     gDt.Columns.Add("id") 
     gDt.Columns.Add("questionid") 
     gDt.Columns.Add("serial") 
     gDt.Columns.Add("direction") 
     Dim dr As DataRow 


      myXML ='<my above shared XML>' 
      Dim xmlDoc As New XmlDocument 
      xmlDoc.LoadXml(myXML) 
      dr = gDt.NewRow 
      dr("serial") = 1 
      dr("id") = xmlDoc.Attributes("id").Value 
      dr("direction") = xmlDoc("direction").InnerText 
      gDt.Rows.Add(dr) 

但是那不工作,因爲我想

+1

您可以從[XmlDocument]中開始[加載字符串](https://msdn.microsoft.com/en-us/library/system.xml.xmldocument.loadxml(v = vs.110).aspx) ](https://msdn.microsoft.com/en-us/library/system.xml.xmldocument(v = vs.110)的.aspx)。 –

+0

@ the_lotus請檢查我的編輯 –

+0

你是指「但不工作」和「那些工作根本不工作,我願意」是什麼意思? –

回答

2

有很多方法在.NET解析XML,如使用序列類或XmlReader類之一,但兩個最流行的選擇將它解析與XElementXmlDocument。例如:

Dim input As String = "<trueFalseQuestion id=""585"" status=""correct"" maxPoints=""10"" maxAttempts=""1"" awardedPoints=""10"" usedAttempts=""1"" xmlns=""http://www.ispringsolutions.com/ispring/quizbuilder/quizresults""><direction>You have NO control over how you treat customers.</direction><answers correctAnswerIndex=""1"" userAnswerIndex=""1""><answer>True</answer><answer>False</answer></answers></trueFalseQuestion>" 
Dim element As XElement = XElement.Parse(input) 
Dim id As String = [email protected] 

或者:

Dim input As String = "<trueFalseQuestion id=""585"" status=""correct"" maxPoints=""10"" maxAttempts=""1"" awardedPoints=""10"" usedAttempts=""1"" xmlns=""http://www.ispringsolutions.com/ispring/quizbuilder/quizresults""><direction>You have NO control over how you treat customers.</direction><answers correctAnswerIndex=""1"" userAnswerIndex=""1""><answer>True</answer><answer>False</answer></answers></trueFalseQuestion>" 
Dim doc As New XmlDocument() 
doc.LoadXml(input) 
Dim nsmgr As New XmlNamespaceManager(doc.NameTable) 
nsmgr.AddNamespace("q", "http://www.ispringsolutions.com/ispring/quizbuilder/quizresults") 
Dim id As String = doc.SelectSingleNode("/q:trueFalseQuestion/@id", nsmgr).InnerText 

基於更新後的問題,它看起來像您遇到的問題是你沒有正確指定命名空間。如果使用XElement,則它更容易(即鬆散),但是當您使用XPath選擇XmlDocument中的節點時,即使它是文檔頂級元素上的默認名稱空間,也需要指定每個名稱空間。

+0

非常感謝那些信息..但是如果你不介意的話,還有一個問題,如何在不知道「/ q:trueFalseQuestion/@ id」部分的情況下選擇該節點。我的意思是我只需要選擇整個節點在字符串下。另外它的not true FalseQuestion總是.. –

+1

XmlDocument對象的'DocumentElement'屬性返回文檔中的頂級元素。除此之外,您可以通過'ChildNodes'屬性遍歷元素列表。 –

+0

多謝老闆..你救了我一天! –