VB 2010 (45) XDocument class

XDocument class

https://docs.microsoft.com/zh-cn/dotnet/api/system.xml.linq.xdocument?view=netframework-4.8

Represents an XML document.

The following example creates a document, and then adds a comment and an element to it. Then, it uses the query results to write another document.

Dim srcTree As XDocument = _   
    <?xml version="1.0" encoding="utf-8" standalone="yes"?>  
        <!--This is a comment-->  
        <Root>  
            <Child1>data1</Child1>  
            <Child2>data2</Child2>  
            <Child3>data3</Child3>  
            <Child2>data4</Child2>  
            <Info5>info5</Info5>  
            <Info6>info6</Info6>  
            <Info7>info7</Info7>  
            <Info8>info8</Info8>  
        </Root>  
Dim doc As XDocument = _   
    <?xml version="1.0" encoding="utf-8" standalone="yes"?>  
        <!--This is a comment-->  
        <Root>  
            <%= From el In srcTree.<Root>.Elements _  
                Where CStr(el).StartsWith("data") _  
                Select el %>  
        </Root>  
Console.WriteLine(doc)

This example produces the following output:

<!--This is a comment-->  
<Root>  
  <Child1>data1</Child1>  
  <Child2>data2</Child2>  
  <Child3>data3</Child3>  
  <Child2>data4</Child2>  
</Root>

    XDocument replaces the XmlDocument object before the introduction of LINQ. The XDocument object is easier to use when dealing with XML documents, although it does not follow any international standards. It uses other new objects in this namespace, such as XNamespace, XComment, XElement, and XAttribute objects.

Constructor

 
XDocument()

Initialize   a new instance of the XDocument class.

XDocument(Object[])

Initialize  a new instance of the XDocument class with the specified content  .

XDocument(XDeclaration, Object[])

 Initialize  a new instance of the XDeclaration class with the specified  XDocument and content  .

XDocument(XDocument)

 Initialize  a new instance of the XDocument class from the existing  XDocument object  .

method 

Load(Stream)

Create a new XDocument  instance using the specified stream  .

Load(Stream, LoadOptions)

Use the specified stream to create a new  XDocument  instance, you can also choose to leave blank, set the base URI and retain the line information.

Load(String)

Create a new XDocument from the file  .

Load(String, LoadOptions)

To create a new XDocument from a file  , you can also choose to keep blank and line information and set the base URI.

Load(TextReader)

 Create a new  TextReader from  XDocument .

Load(TextReader, LoadOptions)

 Create a new  TextReader from  XDocument , you can also choose to keep blank and line information and set the base URI.

Load(XmlReader)

 Create a new  XmlReader from  XDocument .

Load(XmlReader, LoadOptions)

 Load  XmlReader from  XDocument , you can also choose to set the base URI and retain the line information.

Save(Stream)

 Output this  XDocument to the specified  Stream .

Save(Stream, SaveOptions)

 Output this  XDocument to the specified  Stream , (optional) and specify the formatting behavior.

Save(String)

Serialize this  XDocument  to a file, if the file exists, overwrite the existing file.

Save(String, SaveOptions)

 Serialize this  XDocument into a file, and optionally disable formatting.

Save(TextWriter)

 Serialize  this  XDocument to TextWriter .

Save(TextWriter, SaveOptions)

 Serialize  this  XDocument to TextWriter , and optionally disable formatting.

Save(XmlWriter)

 Serialize  this  XDocument to XmlWriter .

Load method example:

Dim  xdoc  A$  XDocument  =  XDocument.Load .("C:\Hamlet.xml")
Console .WriteLine (xdoc.Root.Name .ToString ())
Console .WriteLine (xdoc ..Root .HasAttributes .ToString ())
Save方法示例:
Dim xdoc As XDocument = XDocument.Load("C:\Hamlet.xml")
xdoc.Save ( "c : \CopyOfHamlet . xml")

An example:

Module Module1

    Sub Main()
        testXdocument()
    End Sub

    Private Sub testXdocument()
        Dim xDoc As XDocument = XDocument.Load("~\test.xml")
        Console.WriteLine(xDoc.Root.Name.ToString)
        Console.WriteLine(xDoc.Root.HasElements.ToString)

        xDoc.Save("~\testcopy.xml")

        Console.Read()

    End Sub
End Module
'附:text.xml内容
'<?xml version="1.0" encoding="utf-8" ?>
'<MovieOrderDump>
' <FilmOrderList>
'    <multiFilmOrders>
'        <FilmOrder filmID="101">
'           <name>Grease</name>
'           <quantity>lO</quantity>
'        </FilmOrder>
'      <FilmOrder filmID="102">
’        <quantity>11</quantity>
'      </FilmOrder>
'        <FilmOrder filmID="l03">
'           <name>Star Vlars</name>
'           <quantity>l2</quantity>
'        </FilmOrder>
'     </multiFilmOrders>
'  </FilmOrderList>
'</MovieOrderDump>

Published 146 original articles · praised 0 · visits 2744

Guess you like

Origin blog.csdn.net/ngbshzhn/article/details/105572974