开发者

XmlDocument.InnerXml is null, but InnerText is not

I'm using XmlDocument and XmlElement to build a simple (but large) XML document that looks something like:

<Widgets>
    <Widget>
        <Stuff>foo</Stuff>
        <MoreStuff>bar</MoreStuff>...lots more child nodes
    </Widget>
    <Widget>...lots more Widget nodes
</Widgets>

My problem is that when I'm done building the XML, the XmlDocument.InnerXml is null, but the InnerText still shows all the text of all the child nodes.

Has anyone ever seen a problem like this before? What kind of input data would cause these symptoms? I expected the XmlDocument to just throw an exception if it was given bad data.

Note: I'm pretty sure this is related to the input data as I can only reproduce it against certain data sets. I also tried escaping the data with SecurityElement.Escape but it made no difference.

EDIT Here's the code I'm using to build the XML:

Private Function BuildXml(widgets as ICollection(Of MyNamespace.Widget)) As String
    Dim xDoc As New XmlDocument
    Dim parentNode As XmlElement = xDoc.CreateElement("Widgets")
    xDoc.AppendChild(parentNode)

    For Each w as Widget in widgets
        Dim widgetNode As XmlElement = xDoc.CreateElement("Widget")
        widgetNode.AppendChild(CreateElement(x开发者_如何学PythonDoc, w.Stuff, "Stuff"))
        // lots more...
        parentNode.AppendChild(widgetNode)
    Next

    Return xDoc.OuterXml
End Function

Private Function CreateElement(ByVal xDoc As XmlDocument, ByVal value As String, ByVal elementName As String) As XmlElement
    Dim element As XmlElement = xDoc.CreateElement(elementName)
    element.InnerText = System.Security.SecurityElement.Escape(value)

    Return element
End Function


have you tried normalizing the XMLDocument after you add all the elements and before trying to read the InnerXml or OuterXML?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜