Skip to content

Commit 83a7d69

Browse files
Removed poor coding style in schema validation (#8155)
* Removed poor coding style in schema validation getSchema/LoadDocumentWithSchemaValidation * Returned null on error (versus throwing an exception) -- now just falls back on exceptions * Used a catch in the success path -- removed try/catch * Removed .NET 1.0 code (below UTF8.GetBytes/MemoryStream) and implemented using simpler, .NET 1.1 StringReader byte[] byteArray = Encoding.UTF8.GetBytes(xml); MemoryStream stream = new MemoryStream(byteArray); * Added error handling for FileNotFoundException @MSDN-WhiteKnight and I agreed on a cleaner example than the original that included a check for FileNotFoundException in the get schema and get xml code. * Update snippets/csharp/System.Xml/XmlDocument/Overview/xmlhelpermethods.cs Co-authored-by: MSDN.WhiteKnight <[email protected]> * Update snippets/csharp/System.Xml/XmlDocument/Overview/xmlhelpermethods.cs Co-authored-by: MSDN.WhiteKnight <[email protected]> * Update snippets/csharp/System.Xml/XmlDocument/Overview/xmlhelpermethods.cs Co-authored-by: MSDN.WhiteKnight <[email protected]> Co-authored-by: MSDN.WhiteKnight <[email protected]>
1 parent 14d213d commit 83a7d69

File tree

1 file changed

+37
-38
lines changed

1 file changed

+37
-38
lines changed

snippets/csharp/System.Xml/XmlDocument/Overview/xmlhelpermethods.cs

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ public void SaveXML(XmlDocument doc)
5757
#region Validate XML against a Schema
5858

5959
//<Snippet2>
60-
6160
//************************************************************************************
6261
//
6362
// Helper method that generates an XML string.
@@ -82,7 +81,7 @@ private string generateXMLString()
8281
"</books>";
8382
return xml;
8483
}
85-
84+
8685
//************************************************************************************
8786
//
8887
// Associate the schema with XML. Then, load the XML and validate it against
@@ -91,45 +90,40 @@ private string generateXMLString()
9190
//************************************************************************************
9291
public XmlDocument LoadDocumentWithSchemaValidation(bool generateXML, bool generateSchema)
9392
{
94-
XmlReader reader;
95-
93+
XmlReader reader = null;
9694
XmlReaderSettings settings = new XmlReaderSettings();
97-
9895
// Helper method to retrieve schema.
9996
XmlSchema schema = getSchema(generateSchema);
10097

101-
if (schema == null)
102-
{
103-
return null;
104-
}
105-
10698
settings.Schemas.Add(schema);
107-
10899
settings.ValidationEventHandler += ValidationCallback;
109100
settings.ValidationFlags =
110101
settings.ValidationFlags | XmlSchemaValidationFlags.ReportValidationWarnings;
111102
settings.ValidationType = ValidationType.Schema;
112-
113-
try
114-
{
115-
reader = XmlReader.Create("booksData.xml", settings);
116-
}
117-
catch (System.IO.FileNotFoundException)
103+
if (!generateXML)
118104
{
119-
if (generateXML)
105+
try
120106
{
121-
string xml = generateXMLString();
122-
byte[] byteArray = Encoding.UTF8.GetBytes(xml);
123-
MemoryStream stream = new MemoryStream(byteArray);
124-
reader = XmlReader.Create(stream, settings);
107+
reader = XmlReader.Create("booksData.xml", settings);
125108
}
126-
else
109+
catch (FileNotFoundException ex)
127110
{
128-
return null;
111+
Console.WriteLine(
112+
$"XML file not found so generating: {ex.Message}");
113+
generateXML = true;
129114
}
130115
}
131116

117+
if (generateXML)
118+
{
119+
string xml = generateXMLString();
120+
StringReader stringReader = new StringReader(xml);
121+
122+
reader = XmlReader.Create(stringReader, settings);
123+
}
124+
132125
XmlDocument doc = new XmlDocument();
126+
133127
doc.PreserveWhitespace = true;
134128
doc.Load(reader);
135129
reader.Close();
@@ -178,26 +172,31 @@ private string generateXMLSchema()
178172
private XmlSchema getSchema(bool generateSchema)
179173
{
180174
XmlSchemaSet xs = new XmlSchemaSet();
181-
XmlSchema schema;
182-
try
183-
{
184-
schema = xs.Add("http://www.contoso.com/books", "booksData.xsd");
185-
}
186-
catch (System.IO.FileNotFoundException)
175+
XmlSchema schema = null;
176+
177+
if (!generateSchema)
187178
{
188-
if (generateSchema)
179+
try
189180
{
190-
string xmlSchemaString = generateXMLSchema();
191-
byte[] byteArray = Encoding.UTF8.GetBytes(xmlSchemaString);
192-
MemoryStream stream = new MemoryStream(byteArray);
193-
XmlReader reader = XmlReader.Create(stream);
194-
schema = xs.Add("http://www.contoso.com/books", reader);
181+
schema = xs.Add("http://www.contoso.com/books", "booksData.xsd");
195182
}
196-
else
183+
catch (FileNotFoundException ex)
197184
{
198-
return null;
185+
Console.WriteLine(
186+
$"XSD file not found so generating: {ex.Message}");
187+
generateSchema = true;
199188
}
200189
}
190+
191+
if (generateSchema)
192+
{
193+
string xmlSchemaString = generateXMLSchema();
194+
StringReader stringReader = new StringReader(xmlSchemaString);
195+
XmlReader reader = XmlReader.Create(stringReader);
196+
197+
schema = xs.Add("http://www.contoso.com/books", reader);
198+
}
199+
201200
return schema;
202201
}
203202

0 commit comments

Comments
 (0)