在 VB.NET 中将字符串转换为 XML 节点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31519329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 19:18:43  来源:igfitidea点击:

Converting string to XML node in VB.NET

asp.netxmlvb.net

提问by Sandeep Thomas

I've an XML string in database column like this

我在数据库列中有一个像这样的 XML 字符串

<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>

But I need to do XML operations on this string like select its name, attributes values,inner text etc. How can I make this possible from this string

但是我需要对这个字符串做 XML 操作,比如选择它的名称、属性值、内部文本等。我​​怎样才能从这个字符串中做到这一点

EDIT

编辑

Im sharing the code snippet I tried, but not working

我分享了我试过的代码片段,但没有用

        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)

But thats not working at all as I wish

但这根本不像我希望的那样工作

回答by Steven Doggart

There are many ways to parse XML in .NET, such as using one of the serialization classes or the XmlReaderclass, but the two most popular options would be to parse it with either XElementor XmlDocument. For instance:

在 .NET 中有很多解析 XML 的方法,例如使用序列化类或XmlReader类之一,但两个最流行的选择是使用XElement或解析它XmlDocument。例如:

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 = element.@id

Or:

或者:

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

Based on your updated question, it looks like the trouble you were having is that you weren't properly specifying the namespace. If you use XElement, it's much more forgiving (i.e. loose), but when you use XPath to select nodes in an XmlDocument, you need to specify every namespace, even when it's the default namespace on the top-level element of the document.

根据您更新的问题,您遇到的问题似乎是您没有正确指定命名空间。如果您使用XElement,它会更加宽容(即松散),但是当您使用 XPath 在 中选择节点时XmlDocument,您需要指定每个名称空间,即使它是文档顶级元素上的默认名称空间。