Udostępnij za pośrednictwem


Praca z danych xml typ danych w aplikacji klient programu Visual Studio

This feature will be removed in a future version of Microsoft SQL Server. Avoid using this feature in new development work, and plan to modify applications that currently use this feature.

The xml data type lets you store XML fragments, such as an XML wystąpienie missing a single top-poziom element, and valid XML documents in a SQL Server database. Z powodu ta cecha projektowania xml wystąpienia typu danych musi być mapowana w Visual Studio 2005 na tablicy System.Xml.XmlNode zwracane w postaci zamiastSystem.Xml.XmlDocument.Pofragmentowane XML nie jest obsługiwana.

W pracy bezpośrednio z tablicy XmlNode jest zawarty w xml Typ danych wartości wystąpienia, można zauważyć różnice sposobów InnerXml and OuterXml właściwości elementu członkowskiego pracy, szczególnie przypadek gdy xml Typ danych formularzy wystąpienie prawidłowym dokumentem XML, takie jak ma jeden element członkowski najwyższego poziom.

Na przykład, załóżmy, że masz następujące wiersze kodu, który inicjuje nowe wystąpienie SQL Server punkt końcowy)MyServer.sql_endpoint) jako serwer proxy sieci Web, który jest metoda sieci Web (GetSomeXml) zwracającą xml Typ danych wartości wystąpienie:

MyServer.sql_endpoint proxy = new MyServer.sql_endpoint();
proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
SqlXmlDt = proxy.MyServerdboGetSomeXml();
System.Xml.XmlNode[] nodeArr = SqlXmlDt.Any;
string xmlJustChildren = nodeArr[0].InnerXml;
string xmlWithRoot = nodeArr[0].OuterXml;

The xml data type row value returned then has the following data:

<root><child/><child/></root>

Jeśli InnerXml i OuterXml właściwości nodeArr[0] następnie przypisano się parą (zmienne) ciągxmlJustChildren i xmlWithRoot), jak pokazano w poprzednim kodzie wartości nodeArr[0].InnerXml zawiera tylko węzłów, które są zawarte w ciąg bieżącego elementu (oba <child/> elementy, ale nie <root> samego elementu), a nodeArr[0].OuterXml działa w zamierzony sposób: w tym wszystkie węzły w tablicy XmlNodes ( <child/> elementy, a także <root> element).

Należy zauważyć, że to zachowanie różni się od co można zwykle stwierdzić Jeśli często pracujesz z XmlDocument ponieważ tej klasy implementuje InnerXml and OuterXml właściwości inaczej.Dla XmlDocument wystąpienie, instancję dokumentu działa jako elementy otoki, aby wszystkie XmlNodes w dokumencie.Obejmuje to węzła najwyższego poziom głównego oraz elementów DTD wbudowany lub schematów może być w dokumencie.W rezultacie zawartość XmlDocument.InnerXml jest taka sama, jak XmlDocument.OuterXml.

Because of these implementation specifics, using System.Xml.XmlDocumentFragment is a good alternative for working with SQL Serverxml data type instances in client applications that work with Native XML Web Service.The XmlDocumentFragment class will be more familiar to developers who are used to using XmlDocument, and XmlDocumentFragment accepts an array of XmlNode without issue.

The following sections provide code and overview of how to use the XmlDocumentFragment to work with SQL Serverxml data type instance values in client applications.

Przetwarzanie danych wyjściowych przy użyciu XmlDocumentFragment

Następujące wiersze kodu pokazują, jak wprowadzić tablicę XmlNode do XmlDocumentFragment, a następnie wybierz węzły z fragmentu za pomocą wyrażenie XPath.

System.Xml.XmlDocumentFragment fragOut = SqlXmlDt.Any[0].OwnerDocument.CreateDocumentFragment();

//  Loop over your XmlNode array and populate your XmlDocumentFragment.
foreach (System.Xml.XmlNode xmlnode in SqlXmlDt.Any)
{
    fragOut.AppendChild(xmlnode);
}

//  Loop over your XPath expression/selection for results.
foreach (System.Xml.XmlNode xmlpath in fragOut.SelectNodes("//bar"))
{
    System.Console.WriteLine(xmlpath.OuterXml);
}

Budowanie wprowadzania za pomocą ciąg znaków przy użyciu XmlDocumentFragment

Poniżej pokazano, jak zbudować Tablica wejściowa XmlNode przy użyciu przypisania ciąg InnerXml Właściwość XmlDocumentFragment.

//  Create an owning XmlDocument
System.Xml.XmlDocument xmldoc = new System.Xml.XmlDocument();

//  Create your XmlDocumentFragment.
System.Xml.XmlDocumentFragment fragIn = xmldoc.CreateDocumentFragment();

//  Fill the XmlDocumentFragment with a string.
fragIn.InnerXml =
"  <a>" +
"    <b>inputvalue</b>" +
"  </a>" +
"  topstuff" +
"  <b/>";

//  Create an XmlNode array (should never require more than one element).
System.Xml.XmlNode[] xmlnodes = new System.Xml.XmlNode[1];

//  Put the XmlDocumentFragment in the array and fill your XmlDt
xmlnodes[0] = (System.Xml.XmlNode) fragIn;
SqlXmlDt.Any = xmlnodes;

Tworzenie danych wejściowych z pliku przy użyciu XmlDocumentFragment

The XmlDocumentFragment class is more limited than the XmlDocument class when it comes to how to populating an wystąpienie.W poniższym przykładzie przedstawiono sposób wypełniania XmlDocumentFragment wystąpienie z pliku przy użyciu System.Xml.XmlReader.

//  Create an owning XmlDocument.
System.Xml.XmlDocument xmldoc = new System.Xml.XmlDocument();

//  Create your XmlDocumentFragment.
System.Xml.XmlDocumentFragment fragIn = xmldoc.CreateDocumentFragment();

//  Build an XmlReader from the file.
System.Xml.XmlReaderSettings rs = new System.Xml.XmlReaderSettings();
rs.ConformanceLevel = System.Xml.ConformanceLevel.Fragment;
System.Xml.XmlReader reader = System.Xml.XmlReader.Create("c:\\file.xml", rs);

//  Populate the fragment with the nodes from the XmlReader.
System.Xml.XmlNode child;
while (null != (child = xmldoc.ReadNode(reader)))
     fragIn.AppendChild(child);

//  Create your XmlNode array (should never require more than one element)
    System.Xml.XmlNode[] xmlnodes = new System.Xml.XmlNode[1];

//  Put the XmlDocumentFragment in the array and fill our XmlDt.
xmlnodes[0] = (System.Xml.XmlNode) fragIn;
SqlXmlDt.Any = xmlnodes;