XDocument Konstruktory

Definicja

Inicjuje nowe wystąpienie klasy XDocument.

Przeciążenia

Nazwa Opis
XDocument()

Inicjuje nowe wystąpienie klasy XDocument.

XDocument(Object[])

Inicjuje nowe wystąpienie XDocument klasy z określoną zawartością.

XDocument(XDocument)

Inicjuje nowe wystąpienie XDocument klasy z istniejącego XDocument obiektu.

XDocument(XDeclaration, Object[])

Inicjuje nowe wystąpienie XDocument klasy z określoną wartością XDeclaration i zawartością.

Przykłady

Poniższy przykład tworzy dokument, a następnie dodaje do niego komentarz i element. Następnie komponuje inny dokument przy użyciu wyników zapytania.

XDocument srcTree = new XDocument(
    new XComment("This is a comment"),
    new XElement("Root",
        new XElement("Child1", "data1"),
        new XElement("Child2", "data2"),
        new XElement("Child3", "data3"),
        new XElement("Child2", "data4"),
        new XElement("Info5", "info5"),
        new XElement("Info6", "info6"),
        new XElement("Info7", "info7"),
        new XElement("Info8", "info8")
    )
);

XDocument doc = new XDocument(
    new XComment("This is a comment"),
    new XElement("Root",
        from el in srcTree.Element("Root").Elements()
        where ((string)el).StartsWith("data")
        select el
    )
);
Console.WriteLine(doc);
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)

Ten przykład generuje następujące wyniki:

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

Uwagi

Przeciążone konstruktory umożliwiają utworzenie nowego pustego XDocumentobiektu , utworzenie obiektu XDocument z określoną początkową zawartością oraz utworzenie XDocument obiektu jako kopii innego XDocument obiektu.

Nie ma wielu scenariuszy, które wymagają utworzenia elementu XDocument. Zamiast tego można zwykle tworzyć drzewa XML za pomocą węzła głównego XElement. Jeśli nie masz określonego wymagania dotyczącego tworzenia dokumentu (na przykład dlatego, że musisz utworzyć instrukcje przetwarzania i komentarze na najwyższym poziomie lub musisz obsługiwać typy dokumentów), często wygodniejsze jest użycie XElement jako węzła głównego.

Aby uzyskać szczegółowe informacje o prawidłowej zawartości obiektu XDocument, zobacz Prawidłowa zawartość elementów XElement i XDocument Objects.

XDocument()

Źródło:
XDocument.cs
Źródło:
XDocument.cs
Źródło:
XDocument.cs
Źródło:
XDocument.cs
Źródło:
XDocument.cs

Inicjuje nowe wystąpienie klasy XDocument.

public:
 XDocument();
public XDocument();
Public Sub New ()

Przykłady

Poniższy przykład tworzy nowy dokument, a następnie dodaje do niego komentarz i element.

XDocument doc = new XDocument();
doc.Add(new XComment("This is a comment"));
doc.Add(new XElement("Root", "content"));
Console.WriteLine(doc);
Dim doc As XDocument = New XDocument()
doc.Add(<!--This is a comment-->)
doc.Add(<Root>content</Root>)
Console.WriteLine(doc)

Ten przykład generuje następujące wyniki:

<!--This is a comment-->
<Root>content</Root>

Uwagi

Nie ma wielu scenariuszy, które wymagają utworzenia elementu XDocument. Zamiast tego można zwykle tworzyć drzewa XML za pomocą węzła głównego XElement. Jeśli nie masz określonego wymagania dotyczącego tworzenia dokumentu (na przykład dlatego, że musisz utworzyć instrukcje przetwarzania i komentarze na najwyższym poziomie lub musisz obsługiwać typy dokumentów), często wygodniejsze jest użycie XElement jako węzła głównego.

Aby uzyskać szczegółowe informacje o prawidłowej zawartości obiektu XDocument, zobacz Prawidłowa zawartość elementów XElement i XDocument Objects.

Zobacz też

Dotyczy

XDocument(Object[])

Źródło:
XDocument.cs
Źródło:
XDocument.cs
Źródło:
XDocument.cs
Źródło:
XDocument.cs
Źródło:
XDocument.cs

Inicjuje nowe wystąpienie XDocument klasy z określoną zawartością.

public:
 XDocument(... cli::array <System::Object ^> ^ content);
public XDocument(params object[] content);
public XDocument(params object?[] content);
new System.Xml.Linq.XDocument : obj[] -> System.Xml.Linq.XDocument
Public Sub New (ParamArray content As Object())

Parametry

content
Object[]

Lista parametrów obiektów zawartości do dodania do tego dokumentu.

Przykłady

Poniższy przykład tworzy dokument, a następnie dodaje do niego komentarz i element. Następnie komponuje inny dokument przy użyciu wyników zapytania.

XDocument srcTree = new XDocument(
    new XComment("This is a comment"),
    new XElement("Root",
        new XElement("Child1", "data1"),
        new XElement("Child2", "data2"),
        new XElement("Child3", "data3"),
        new XElement("Child2", "data4"),
        new XElement("Info5", "info5"),
        new XElement("Info6", "info6"),
        new XElement("Info7", "info7"),
        new XElement("Info8", "info8")
    )
);

XDocument doc = new XDocument(
    new XComment("This is a comment"),
    new XElement("Root",
        from el in srcTree.Element("Root").Elements()
        where ((string)el).StartsWith("data")
        select el
    )
);
Console.WriteLine(doc);
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)

Ten przykład generuje następujące wyniki:

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

Uwagi

Nie ma wielu scenariuszy, które wymagają utworzenia elementu XDocument. Zamiast tego można zwykle tworzyć drzewa XML za pomocą węzła głównego XElement. Jeśli nie masz określonego wymagania dotyczącego tworzenia dokumentu (na przykład dlatego, że musisz utworzyć instrukcje przetwarzania i komentarze na najwyższym poziomie lub musisz obsługiwać typy dokumentów), często wygodniejsze jest użycie XElement jako węzła głównego.

Aby uzyskać szczegółowe informacje o prawidłowej zawartości obiektu XDocument, zobacz Prawidłowa zawartość elementów XElement i XDocument Objects.

Zobacz też

Dotyczy

XDocument(XDocument)

Źródło:
XDocument.cs
Źródło:
XDocument.cs
Źródło:
XDocument.cs
Źródło:
XDocument.cs
Źródło:
XDocument.cs

Inicjuje nowe wystąpienie XDocument klasy z istniejącego XDocument obiektu.

public:
 XDocument(System::Xml::Linq::XDocument ^ other);
public XDocument(System.Xml.Linq.XDocument other);
new System.Xml.Linq.XDocument : System.Xml.Linq.XDocument -> System.Xml.Linq.XDocument
Public Sub New (other As XDocument)

Parametry

other
XDocument

Obiekt XDocument , który zostanie skopiowany.

Uwagi

Ten konstruktor służy do tworzenia głębokiej kopii elementu XDocument.

Ten konstruktor przechodzi przez wszystkie węzły i atrybuty w dokumencie określonym w parametrze other i tworzy kopie wszystkich węzłów podczas tworzenia nowo zainicjowanego XDocumentelementu .

Zobacz też

Dotyczy

XDocument(XDeclaration, Object[])

Źródło:
XDocument.cs
Źródło:
XDocument.cs
Źródło:
XDocument.cs
Źródło:
XDocument.cs
Źródło:
XDocument.cs

Inicjuje nowe wystąpienie XDocument klasy z określoną wartością XDeclaration i zawartością.

public:
 XDocument(System::Xml::Linq::XDeclaration ^ declaration, ... cli::array <System::Object ^> ^ content);
public XDocument(System.Xml.Linq.XDeclaration declaration, params object[] content);
public XDocument(System.Xml.Linq.XDeclaration? declaration, params object?[] content);
public XDocument(System.Xml.Linq.XDeclaration? declaration, params object[] content);
new System.Xml.Linq.XDocument : System.Xml.Linq.XDeclaration * obj[] -> System.Xml.Linq.XDocument
Public Sub New (declaration As XDeclaration, ParamArray content As Object())

Parametry

declaration
XDeclaration

Element XDeclaration dla dokumentu.

content
Object[]

Zawartość dokumentu.

Przykłady

W poniższym przykładzie użyto tego konstruktora do utworzenia dokumentu.

XDocument srcTree = new XDocument(
    new XComment("This is a comment"),
    new XElement("Root",
        new XElement("Child1", "data1"),
        new XElement("Child2", "data2"),
        new XElement("Child3", "data3"),
        new XElement("Child2", "data4"),
        new XElement("Info5", "info5"),
        new XElement("Info6", "info6"),
        new XElement("Info7", "info7"),
        new XElement("Info8", "info8")
    )
);

XDocument doc = new XDocument(
    new XDeclaration("1.0", "utf-8", "yes"),
    new XComment("This is a new comment"),
    new XElement("Root",
        from el in srcTree.Element("Root").Elements()
        where ((string)el).StartsWith("data")
        select el
    )
);
doc.Save("Test.xml");
Console.WriteLine(File.ReadAllText("Test.xml"));
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 new comment-->
        <Root>
            <%= From el In srcTree.<Root>.Elements _
                Where CStr(el).StartsWith("data") _
                Select el %>
        </Root>
doc.Save("Test.xml")
Console.WriteLine(File.ReadAllText("Test.xml"))

Ten przykład generuje następujące wyniki:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!--This is a new comment-->
<Root>
  <Child1>data1</Child1>
  <Child2>data2</Child2>
  <Child3>data3</Child3>
  <Child2>data4</Child2>
</Root>

Uwagi

Nie ma wielu scenariuszy, które wymagają utworzenia elementu XDocument. Zamiast tego można zwykle tworzyć drzewa XML za pomocą węzła głównego XElement. Jeśli nie masz określonego wymagania dotyczącego tworzenia dokumentu (na przykład dlatego, że musisz utworzyć instrukcje przetwarzania i komentarze na najwyższym poziomie lub musisz obsługiwać typy dokumentów), często wygodniejsze jest użycie XElement jako węzła głównego.

Aby uzyskać szczegółowe informacje o prawidłowej zawartości obiektu XDocument, zobacz Prawidłowa zawartość elementów XElement i XDocument Objects.

Zobacz też

Dotyczy