
found on http://xkcd.com/221/
Code that helps you code.
1: String.Format("{0:X02}", int.MaxValue);
1: Convert.ToString(int.MaxValue, 2);
1: string data = "52/00000012340056";
1: Regex regex = new Regex(@"0+");
1: Regex regex = new Regex(@"^\d+/0+");
1: string result = regex.Replace(data, String.Empty);
2: // result == "12340056";
1: Regex regex = new Regex(@"^(?<prefix>\d+/)(0+)");
2: string result = regex.Replace(data, "${prefix}");
3: // result == "52/12340056";
1: <books xmlns="urn:books">
2: <book>
3: <author>It's just me</author>
4: <publisheddate>2009-09-25T09:06</publisheddate>
5: </book>
6: </books>
1: <?xml version="1.0" encoding="utf-8"?>
2: <xs:schema targetNamespace="urn:books" xmlns:xs="http://www.w3.org/2001/XMLSchema">
3: <xs:element name="books">
4: <xs:complexType>
5: <xs:sequence>
6: <xs:element name="book">
7: <xs:complexType>
8: <xs:sequence>
9: <xs:element name="author" type="xs:string" />
10: <xs:element name="publisheddate" type="xs:dateTime" />
11: </xs:sequence>
12: </xs:complexType>
13: </xs:element>
14: </xs:sequence>
15: </xs:complexType>
16: </xs:element>
17: </xs:schema>
1: // load xml
2: XmlDocument xmlDoc = new XmlDocument();
3: xmlDoc.Load("books.xml");
1: // load schema XmlSchema schema; using(XmlReader schemaReader = XmlReader.Create("books.xsd")) schema = XmlSchema.Read(schemaReader, (p, q) => if(q.Exception != null) throw q.Exception);
1: // add schema xmlDoc.Schemas.Add(xmlDoc.DocumentElement.NamespaceUri, schema);
1: // validate document
2: xmlDoc.Validate((p, q) => if(q.Exception != null) throw q.Exception);
1: // loading schema
2: XmlSchemaSet schemaSet = new XmlSchemaSet();
3: schemaSet.Add("urn:books", "books.xsd");
4: XmlReaderSettings settings = new XmlReaderSettings();
5: settings.ValidationType = ValidationType.Schema;
6: settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
7: settings.Schemas = schemaSet;
8: settings.ValidationEventHandler += (p, q) =>
9: {
10: if (args.Severity==XmlSeverityType.Warning)
11: Console.WriteLine("Warning: Matching schema not found. No validation occurred." + args.Message);
12: else if(q.Exception != null)
13: throw q.Exception;
14: else
15: Console.WriteLine("\tValidation error: " + args.Message);
16: };
17: // create reader
18: using(XmlReader reader = XmlReader.Create("books.xml", settings))
19: {
20: // validate
21: while(reader.Read());
22: }
<books xmlns="urn:books"> <book> <author>It's just me</author> <publisheddate>2009-09-25T09:06</publisheddate> </book> </books>
XmlDocument doc = new XmlDocument(); doc.Load("books.xml"); // just build xPath for testing StringBuilder xPath = new StringBuilder(); XmlNode someNode = doc.DocumentElement.FirstChild.FirstChild; do { xPath.Insert(0, someNode.LocalName); xPath.Insert(0, "/"); someNode = someNode.ParentNode; } while (someNode != null && someNode.NodeType != XmlNodeType.Document); // xPath == "/books/book/author" XmlNode author = doc.DocumentElement.SelectSingleNode(xPath.ToString());
XmlNamespaceManager manager = new XmlNamespaceManager(doc.NameTable); // try to access with XmlNamespaceManager XmlNode author = doc.DocumentElement.SelectSingleNode(xPath.ToString(), manager);
XmlNamespaceManager manager = new XmlNamespaceManager(doc.NameTable); // try to access with XmlNamespaceManager XmlNode author = doc.DocumentElement.SelectSingleNode(xPath.ToString(), manager); So try this: string prefix = "mySample"; if (String.IsNullOrEmpty(manager.LookupPrefix(doc.DocumentElement.NamespaceURI))) { // namespace not added manager.AddNamespace(prefix, doc.DocumentElement.NamespaceURI); } // injecting the namespace xPath.Replace("/", String.Format("/{0}:", prefix)); // xPath is now "/{mySample}:books/{mySample}:book/{mySample}:author" author = doc.DocumentElement.SelectSingleNode(xPath.ToString(), manager);
FileOptions.DeleteOnClosevalue as parameter of the
FileStreamconstructor, this will keep you handles clean.
using (FileStream fStream = new FileStream(Path.GetTempFileName(), FileMode.Create, FileAccess.Write, FileShare.None, 4096, FileOptions.DeleteOnClose))) { // do work }
x = x ^ y; y = y ^ x; x = x ^ y;
x ^= y; y ^= x; x ^= y;
x ^= y; y ^= x; x ^= y;