Aracılığıyla paylaş


Veri türü, Visual Studio istemci uygulamalarında xml ile çalışma

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 instance missing a single top-level element, and valid XML documents in a SQL Server database.Bu tasarım özellikleri nedeniylexmlveri türü örneklerini gerekir eşlenenVisual Studio 2005bir dizi içinSystem.Xml.XmlNode yerine System.Xml.XmlDocument döndürülen. Parçalanmış XML desteklemez.

Bir dizi ile çalışmaXmlNode içerdiği tarafından xmlörnek değer veri türü yöntemleri arasındaki farkları göreceksiniz InnerXmlOuterXml üye özelliklerini iş ve, özellikle durumda burada xmlda var. bir tek üst düzey öğe gibi geçerli bir XML belgesi veri türü örneği oluşturur

Örneğin, aşağıdaki satırları yeni örnek başlatan kod sahip varsayalım birSQL Serverbitiş noktası ( MyServer.sql_endpoint) ( GetSomeXml), bir döndüren bir Web yöntem olan Web proxy olarak xmlsatır örnek değerinin veri türü:

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>

,InnerXmlveOuterXmlözelliklerinodeArr[0]sonra öğrenmek için dize değişkenleri çifti atanır ( xmlJustChildrenve xmlWithRoot) değerinin önceki kodda gösterildiği gibinodeArr[0].InnerXmlyalnızca geçerli öğenin içinde bulunan düğümler içerir (her ikisi de <child/>öğeleri değil ama <root>öğenin kendisi), venodeArr[0].OuterXmlbeklendiği gibi çalışır: XmlNodes(the<child/>elements and also the<root>element). dizideki tüm düğümleri de dahil olmak üzere

Not Bu davranış, farklı ne genellikle görebilirsiniz, çalışma ile daha sık gelenXmlDocument için farklı ve InnerXmlOuterXml özellik bu sınıf uygulayan. İçinXmlDocument örnekler, belge örnek tüm XmlNodes . biçiminde kapsayıcı öğeler olarak çalışır. Bu belgede en üst düzey kök düğümü ve DTD'ler satır içi şemalar sizde içerir.Nedenle, içeriğiXmlDocument.InnerXmlXmlDocument.OuterXmlaynı olup.

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.

XmlDocumentFragment kullanarak işlem çıkışı

Aşağıdaki kod satırlarını gösteren bir dizi yerleştirmeXmlNodeiçine birXmlDocumentFragment, bir XPath ifadesi. kullanarak düğümler parçası seçin ve

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);
}

XmlDocumentFragment kullanarak bir dize ile bina giriş

Giriş bir dizi oluşturmak için aşağıdaki gösterirXmlNodekullanarak bir dize atamayaInnerXmlözellikXmlDocumentFragment.

//  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;

XmlDocumentFragment kullanarak bir dosyadan oluşturma giriş

The XmlDocumentFragment class is more limited than the XmlDocument class when it comes to how to populating an instance.Doldurmak için aşağıdaki örnek gösterir birXmlDocumentFragmentörnek bir dosya kullanarakSystem.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;