Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!--<snippet3>-->
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="urn:bookstore-schema"
elementFormDefault="qualified"
targetNamespace="urn:bookstore-schema">

<xsd:element name="bookstore" type="bookstoreType"/>

<xsd:complexType name="bookstoreType">
<xsd:sequence maxOccurs="unbounded">
<xsd:element name="book" type="bookType"/>
</xsd:sequence>
</xsd:complexType>

<xsd:complexType name="bookType">
<xsd:sequence>
<xsd:element name="title" type="xsd:string"/>
<xsd:element name="author" type="authorName"/>
<xsd:element name="price" type="xsd:decimal"/>
</xsd:sequence>
<xsd:attribute name="genre" type="xsd:string"/>
</xsd:complexType>

<xsd:complexType name="authorName">
<xsd:sequence>
<xsd:element name="first-name" type="xsd:string"/>
<xsd:element name="last-name" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>

</xsd:schema>
<!--</snippet3>-->
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!--<snippet2>-->
<?xml version='1.0'?>
<bookstore xmlns="urn:bookstore-schema">
<book>
<author>
<first-name>Benjamin</first-name>
<last-name>Franklin</last-name>
</author>
</book>
<book genre="novel">
<title>The Confidence Man</title>
<author>
<first-name>Herman</first-name>
<last-name>Melville</last-name>
</author>
<price>11.99</price>
</book>
<book genre="philosophy">
<title>The Gorgias</title>
<author>
<name>Plato</name>
</author>
<price>9.99</price>
</book>
</bookstore>
<!--</snippet2>-->
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Xml;

public class Sample1
{
public static void Main()
{
// <snippet1>
// Set the reader settings.
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreComments = true;
settings.IgnoreProcessingInstructions = true;
settings.IgnoreWhitespace = true;
// </snippet1>

//<snippet2>
// Create a resolver with default credentials.
XmlUrlResolver resolver = new XmlUrlResolver();
resolver.Credentials = System.Net.CredentialCache.DefaultCredentials;

// Set the reader settings object to use the resolver.
settings.XmlResolver = resolver;

// Create the XmlReader object.
XmlReader reader = XmlReader.Create("http://ServerName/data/books.xml", settings);
// </snippet2>

// Parse the file.
while (reader.Read()) ;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<!--<snippet9>-->
<employee xmlns="urn:empl-hire">
<ID>12365</ID>
<hire-date>2003-01-08</hire-date>
<title>Accountant</title>
</employee>
<!--</snippet9>-->
14 changes: 14 additions & 0 deletions snippets/csharp/System.Xml/XmlReader/Overview/xml/hireDate.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!--<snippet10>-->
<?xml version="1.0"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="urn:empl-hire" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="employee">
<xs:complexType>
<xs:sequence>
<xs:element name="ID" type="xs:unsignedShort" />
<xs:element name="hire-date" type="xs:date" />
<xs:element name="title" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
<!--</snippet10>-->
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Library</OutputType>
<RootNamespace>vb</RootNamespace>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
' <Snippet1>
Imports System.Xml
Imports System.Xml.Schema
Imports System.IO

Public Module Sample
Public Sub Main()

' Create the XmlSchemaSet class.
Dim sc as XmlSchemaSet = new XmlSchemaSet()

' Add the schema to the collection.
sc.Add("urn:bookstore-schema", "books.xsd")

' Set the validation settings.
Dim settings as XmlReaderSettings = new XmlReaderSettings()
settings.ValidationType = ValidationType.Schema
settings.Schemas = sc
AddHandler settings.ValidationEventHandler, AddressOf ValidationCallBack

' Create the XmlReader object.
Dim reader as XmlReader = XmlReader.Create("booksSchemaFail.xml", settings)

' Parse the file.
While reader.Read()
End While

End Sub

' Display any validation errors.
Private Sub ValidationCallBack(sender as object, e as ValidationEventArgs)
Console.WriteLine($"Validation Error:{vbCrLf} {e.Message}")
Console.WriteLine()
End Sub
End Module
' The example displays output like the following:
' Validation Error:
' The element 'book' in namespace 'urn:bookstore-schema' has invalid child element 'author'
' in namespace 'urn:bookstore-schema'. List of possible elements expected: 'title' in
' namespace 'urn:bookstore-schema'.
'
' Validation Error:
' The element 'author' in namespace 'urn:bookstore-schema' has invalid child element 'name'
' in namespace 'urn:bookstore-schema'. List of possible elements expected: 'first-name' in
' namespace 'urn:bookstore-schema'.
' </Snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Library</OutputType>
<RootNamespace>vb</RootNamespace>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
' <Snippet1>
Option Strict On
Option Explicit On
Imports System.Xml

Public Class ElementSample
Public Shared Sub Main()
'Create the XmlDocument.
Dim doc As New XmlDocument()
doc.LoadXml("<?xml version='1.0' ?>" &
"<book genre='novel' ISBN='1-861001-57-5'>" &
"<title>Pride And Prejudice</title>" &
"</book>")

'Display the document element.
Console.WriteLine(doc.DocumentElement.OuterXml)
End Sub
End Class
' </Snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
' <Snippet1>
Option Explicit On
Option Strict On
Imports System.Xml

Public Class LastChildSample
Public Shared Sub Main()

Dim doc As New XmlDocument()
doc.LoadXml("<book ISBN='1-861001-57-5'>" &
"<title>Pride And Prejudice</title>" &
"<price>19.95</price>" &
"</book>")

Dim root As XmlNode = doc.FirstChild

Console.WriteLine("Display the price element...")
Console.WriteLine(root.LastChild.OuterXml)
End Sub
End Class
' </Snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
' <Snippet1>
Imports System.Xml

Public Class NextSiblingSample
Public Shared Sub Main()

Dim doc As XmlDocument = New XmlDocument()
doc.Load("books.xml")

Dim currNode As XmlNode = doc.DocumentElement.FirstChild
Console.WriteLine("First book...")
Console.WriteLine(currNode.OuterXml)

Dim nextNode As XmlNode = currNode.NextSibling
Console.WriteLine(ControlChars.Lf + "Second book...")
Console.WriteLine(nextNode.OuterXml)

End Sub
End Class
' </Snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
' <Snippet1>
Option Explicit On
Option Strict On
Imports System.Xml

Public Class TagSample

Public Shared Sub Main()
'Create the XmlDocument.
Dim doc As New XmlDocument()
doc.Load("books.xml")

'Display all the book titles.
Dim elemList As XmlNodeList = doc.GetElementsByTagName("title")
Dim i As Integer
For i = 0 To elemList.Count - 1
Console.WriteLine(elemList(i).InnerXml)
Next i
End Sub
End Class
' </Snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
' <Snippet1>
Option Strict
Option Explicit

Imports System.IO
Imports System.Xml

Public Class Sample

Public Shared Sub Main()

Dim doc As New XmlDocument()
doc.LoadXml("<book ISBN='1-861001-57-5'>" & _
"<title>Pride And Prejudice</title>" & _
"<price>19.95</price>" & _
"</book>")

Dim root As XmlNode = doc.FirstChild

'Display the contents of the child nodes.
If root.HasChildNodes Then
Dim i As Integer
For i = 0 To root.ChildNodes.Count - 1
Console.WriteLine(root.ChildNodes(i).InnerText)
Next i
End If
End Sub
End Class
' </Snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
' <Snippet1>
Imports System.Xml

Public Class Sample5
Public Shared Sub Main()
Dim doc As XmlDocument = New XmlDocument()
doc.Load("books.xml")

Dim lastNode As XmlNode = doc.DocumentElement.LastChild
Console.WriteLine("Last book...")
Console.WriteLine(lastNode.OuterXml)

Dim prevNode As XmlNode = lastNode.PreviousSibling
Console.WriteLine(ControlChars.Lf + "Previous book...")
Console.WriteLine(prevNode.OuterXml)
End Sub
End Class
' </Snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Public Class Constants

Public Const positionTop As String = "Top"
Public Const positionBottom As String = "Bottom"
Public Const positionAbove As String = "Above selected item"
Public Const positionBelow As String = "Below selected item"
Public Const lengthOfNamespaceDeclaration As Integer = 37
Public Const booksFileName As String = "booksData.xml"
Public Const Title As String = "Title"
Public Const Genre As String = "Genre"
Public Const PubDate As String = "Publish Year"
Public Const Price As String = "Price"
Public Const ISBN As String = "ISBN"
Public Const Condition As String = "Condition"
Public Const Excludes As String = "Excludes"

End Class
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net10.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
' <Snippet1>
Imports System.IO
Imports System.Xml

public class Sample

public shared sub Main()

'Create the XmlDocument.
Dim doc as XmlDocument = new XmlDocument()
doc.Load("booksort.xml")

Dim book as XmlNode
Dim nodeList as XmlNodeList
Dim root as XmlNode = doc.DocumentElement

nodeList=root.SelectNodes("descendant::book[author/last-name='Austen']")

'Change the price on the books.
for each book in nodeList
book.LastChild.InnerText="15.95"
next

Console.WriteLine("Display the modified XML document....")
doc.Save(Console.Out)

end sub
end class
' </Snippet1>
Loading
Loading