English 中文(简体)
VB. NET在XML文件中添加了一个内容,在XML中添加了LINQ。
原标题:VB.NET add an element to the XML document with LINQ to XML

I m 在现有XML doc中添加一个内容,其代码如下:

        Dim theXMLSource As String = Server.MapPath("~/Demo/") & "LabDemo.xml"
    Dim nodeElement As XElement

    Dim attrAndValue As XElement = _
        <LabService>
            <ServiceType>
                <%= txtServiceType.Text.Trim %>
            </ServiceType>
            <Level>
                <%= txtLevel.Text.Trim %>
            </Level>
        </LabService>

    nodeElement.Add(New XElement(attrAndValue))
    nodeElement.Save(theXMLSource)

这是错误的:

System.NullReferenceException: Object reference not set to an instance of an object.

Object reference not set to an instance of an object.

Error line: nodeElement.Add(New XElement(attrAndValue))

我 it倒了这一错误,但我却拿不出错误。 你们能否表明问题是什么? 谢谢。

最佳回答

你们需要装上现有的档案,例如:

Dim theXMLSource As String = Server.MapPath("~/Demo/LabDemo.xml")
Dim document As XDocument = XDocument.Load(theXMLSource)

...

document.Root.Add(attrAndValue)
document.Save(theXMLSource)
问题回答

您定义 内容,但随后在请你说明其方法之前不会立即进行。

首先需要:

Dim nodeElement As New XElement 

“Dim nodeElement As New XElement”

实际上,新式并非要素的有效方法。 即使它穿透了 de(我怀疑是这样),它也会导致卸载。

和S Laks一样,你可以打开现有的档案(我认为档案可能像你在岗位上所说的那样存在)。

你们要么可以使用

document.Root.Add(attrAndValue)

Dim nodeElement As XElement = document.<theXMLroot>(0)

nodeElement.Add(attrAndValue)

之后

document.Save(theXMLSource)

both w或k the same way. since you re using literals, I thought you might want to know the "second way" It s useful mainly because you can change to the where you want to insert the element.

f或 instance

Dim nodeElement As XElement = document.<theXMLroot>.<parent>(0)

Dim nodeElement As XElement = document...<parent>(0)

希望它有助于





相关问题
Is Shared ReadOnly lazyloaded?

I was wondering when I write Shared ReadOnly Variable As DataType = New DataType() Or alternatively Shared ReadOnly Variable As New DataType() Is it lazy loaded or as the instance initializes? ...

Entertaining a baby with VB.NET

I would like to write a little application in VB.NET that will detect a baby s cry. How would I get started with such an application?

Choose Enter Rather than Pressing Ok button

I have many fields in the page and the last field is a dropdown with list of values. When I select an item in a dropdown and press Enter, it doesn t do the "Ok". Instead I have to manually click on Ok ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

Set Select command in code

On button Click I want to Set the Select command of a Gridview. I do this and then databind the grid but it doesn t work. What am i doing wrong? protected void bttnView_Click(object sender, ...

Hover tooltip on specific words in rich text box?

I m trying to create something like a tooltip suddenly hoovering over the mouse pointer when specific words in the richt text box is hovered over. How can this be done?

热门标签