Ejemplo n.º 1
0
 /// <summary>
 /// A cell name which does reject invalid chars.
 /// </summary>
 public StrictCellName(string name)
 {
     this.validated =
         new ScalarOf <string>(() =>
     {
         name = new Normalized(name).AsString();
         if (name.Contains(" ") || name.Contains("\r") || name.Contains("\n"))
         {
             throw new ArgumentException($"Can't use '{name}' as name because it contains whitespaces");
         }
         if (name.Contains("&") || name.Contains("<") || name.Contains(">"))
         {
             throw new ArgumentException($"Can't use '{ name }' as name because it contains illegal chars (&,< or >)");
         }
         return(name);
     }
                               );
 }
Ejemplo n.º 2
0
 /// <summary>
 /// Bytes parsed as XNode.
 /// </summary>
 public BytesXNode(string name, byte[] bytes)
 {
     this.node =
         new ScalarOf <XNode>(() =>
     {
         XDocument doc;
         if (bytes.Length == 0)
         {
             var rootName = new Normalized(name).AsString();
             if (rootName.ToLower().EndsWith(".xml"))
             {
                 rootName = rootName.Substring(0, rootName.Length - 4);
             }
             rootName = rootName.Substring(rootName.LastIndexOf("/"));
             rootName = rootName.TrimStart('/');
             doc      =
                 new XDocument(
                     new XDeclaration("1.0", "UTF-8", "yes"),
                     new XElement(rootName)
                     );
         }
         else
         {
             try
             {
                 using (var reader = new StreamReader(new MemoryStream(bytes)))
                 {
                     doc = XDocument.Load(reader);
                 }
             }
             catch (XmlException ex)
             {
                 throw
                 new ApplicationException(
                     $"Cannot parse this content as XML: '{new TextOf(new InputOf(bytes), Encoding.UTF8).AsString()}'",
                     ex
                     );
             }
         }
         return(doc);
     });
 }