XmlDocument.LoadXml(String) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
从指定的字符串加载 XML 文档。
public:
virtual void LoadXml(System::String ^ xml);
public virtual void LoadXml (string xml);
abstract member LoadXml : string -> unit
override this.LoadXml : string -> unit
Public Overridable Sub LoadXml (xml As String)
参数
- xml
- String
包含要加载的 XML 文档的字符串。
例外
XML 中存在加载或分析错误。 在这种情况下,该文档保留为空。
示例
以下示例将 XML 加载到 XmlDocument
对象中,并将其保存到 文件中。
#using <System.Xml.dll>
using namespace System;
using namespace System::Xml;
int main()
{
// Create the XmlDocument.
XmlDocument^ doc = gcnew XmlDocument;
doc->LoadXml( "<item><name>wrench</name></item>" );
// Add a price element.
XmlElement^ newElem = doc->CreateElement( "price" );
newElem->InnerText = "10.95";
doc->DocumentElement->AppendChild( newElem );
// Save the document to a file and auto-indent the output.
XmlTextWriter^ writer = gcnew XmlTextWriter( "data.xml", nullptr );
writer->Formatting = Formatting::Indented;
doc->Save( writer );
}
using System;
using System.Xml;
public class Sample {
public static void Main() {
// Create the XmlDocument.
XmlDocument doc = new XmlDocument();
doc.LoadXml("<item><name>wrench</name></item>");
// Add a price element.
XmlElement newElem = doc.CreateElement("price");
newElem.InnerText = "10.95";
doc.DocumentElement.AppendChild(newElem);
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
// Save the document to a file and auto-indent the output.
XmlWriter writer = XmlWriter.Create("data.xml", settings);
doc.Save(writer);
}
}
Imports System.Xml
public class Sample
public shared sub Main()
' Create the XmlDocument.
Dim doc as XmlDocument = new XmlDocument()
doc.LoadXml("<item><name>wrench</name></item>")
' Add a price element.
Dim newElem as XmlElement = doc.CreateElement("price")
newElem.InnerText = "10.95"
doc.DocumentElement.AppendChild(newElem)
Dim settings As New XmlWriterSettings()
settings.Indent = True
' Save the document to a file and auto-indent the output.
Dim writer As XmlWriter = XmlWriter.Create("data.xml", settings)
doc.Save(writer)
end sub
end class
注解
默认情况下, LoadXml
方法不保留空格或重要的空格。
此方法分析文档类型定义 (DTD) ,但不执行 DTD 或架构验证。 如果希望进行验证,可以使用 类和 Create 方法创建验证XmlReader实例XmlReaderSettings。 有关详细信息,请参阅 XmlReader 引用页的“备注”部分。
如果要从 Stream、 String、 TextReader或 XmlReader进行加载,请使用 Load 方法而不是此方法。
此方法是文档对象模型 (DOM) 的Microsoft扩展。
适用于
另请参阅
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈