I'm trying to use validation in Spring.NET to validate that a string property of a class is a properly formatted e-mail address. I found the EmailValidator class (source, documentation), but I'm unsure of the syntax needed to actually use it with my class.
I'm trying this:
<v:email id="validate.myClass.MailFrom" test="MailFrom">
<v:message id="myClass.MailFrom" providers="myClassProvider"/>
</v:email>
but I get the error:
The element 'group' in namespace 'http://www.springframework.net/validation' has invalid child element 'email' in namespace 'http://www.springframework.net/validation'. List of possible elements expected: 'validator, required, condition, regex, ref, collection, group, any, exclusive' in namespace 'http://www.springframework.net/validation'.
The same namespace that contains EmailValidator also contains ConditionValidator, RegularExpressionValidator, etc., which seem to correspond to allowed child elements. That doesn't seem to be the case for EmailValidator. What am I missing?
Try something like
<v:validator test="MailFrom"
type="Spring.Validation.Validators.EmailValidator, Spring.Core">
<v:message id="myClass.MailFrom" providers="myClassProvider"/>
</v:validator>
the email validator is a custom validator, which means you have to specify its type. In xml configuration, you use the validator tag as for your own custom validators. See http://www.springframework.net/doc-latest/reference/html/validation.html#d4e3643. I get your point about the other validator classes in the namespace having their own XML element. Maybe the email validator is considered too specific to warrant its own XML element?
Related
I'm using HTMLAgilityPack to grab a bunch of a tags. Some of the have 1 of several classes assigned and some have no class. It's those with no class that I need to grab.
I know that to grab a node by class we can do something like;
.SelectNodes("//table[#class=\"pagelinks\"]");
Similarly you can choose to ignore specific classes or id's with;
.SelectNodes("//table[not(#class=\"pagelinks\")]");
But is there a way to only grab a node when and only when it has no class?
The following code should select when there's no class attribute defined at all:
.SelectNodes("//table[not(#class)]");
This XPath will select tables that either have no class attribute, or that have a class attribute that is entirely whitespace (or blank):
//table[not(normalize-space(#class))]
I'm trying to deserialize an XML with the following element with .NET XmlSerializer :
<SomeElement Foo_x003a_Bar="123"/>
In the target class, there's the following declaration:
class SomeElement
{
//...
[XmlAttribute("Foo_x003a_Bar")]
public string Foo_Bar;
}
The attribute is not being read from XML. In the UnknownAttribute event handler, I see that Foo_x003a_Bar is not recognized, and the list of valid attributes (Args.ExpectedAttributes) instead includes Foo_x005F_x003a_Bar.
What's the deal here, please? 0x5F is the code for the undescore character. Other attributes in the same element/class with names that contain _x0020_ deserialize properly. Why does _x003a_ get some kind of a special treatment?
EDIT: dirty hackery in the form of search/replacing in the XML string before it's parsed helps. But still.
EDIT2: the functions that implement this kind of encoding are XmlConvert:EncodeName, XmlConvert:EncodeLocalName. The latter handles colons, the former doesn't. Looks like they're calling EncodeName...
EDIT3: filed a bug report with Microsoft. Please navigate there and click "I can too" if you can, too :)
Looks like you are right about the treatment for _x<char-code>_ element names (described here), which uses XmlConvert.EncodeLocalName.
Given that you've instantiated your XmlSerializer using the only the type argument, the namespace is assumed to be empty.
What should work in your case is to simply use the decoded element names in your XmlAttributeAttribute settings:
class SomeElement
{
//...
[XmlAttribute("Foo:Bar")]
public string Foo_Bar;
}
One important factor here is casing, the XML node needs to have <Foo_x003A_Bar>, with a capital A.
The encoded node name needs to be consistent with XmlConvert.EncodeLocalName:
XmlConvert.EncodeLocalName("Foo:Bar")
// Foo_x003A_Bar
I need to produce an xml document where all elements and attributes are prefixed with (the same) namespace (I know this is not ideal, but unfortunately is needed for interoperation with InfoPath). Using the .NET XmlSerializer, initialized with the right namepsace and prefix, I generally have no problem generating prefixed xml:
xmlSerializer = new XmlSerializer(typeof(T));
xmlNamespaces = new XmlSerializerNamespaces();
xmlNamespaces.Add("foo", "www.namespace.com");
...
[XmlRoot(Namespace = "www.namespace.com")]
public class label
{
[XmlAttribute(Namespace = "www.namespace.com")]
public string id { get; set; }
[XmlElement(Namespace = "www.namespace.com")]
public string text { get; set; }
}
This generates xml
<foo:label id="0" xmlns:foo="www.namespace.com">
<foo:text>content</foo:text>
</foo:label>
The problem is this: the prefix is applied to everything, except the "id" attribute in the same namespace.
I thought this might be behavior prescribed by W3C, and that attributes declared as belonging to a prefixed element would inherit that prefix. However, it seems that attributes that do not have an associated namespace/prefix do not behave like this - see XML namespaces and attributes and here, which states:
"An attribute never inherits the namespace of its parent element. For that reason an attribute is only in a namespace if it has a proper namespace prefix"
In this case, shouldn't the serializer generate a prefix to show the attribute is in that namespace? Or is this not correct?
Thanks in advance!
MORE INFO: Further investigation (see SamuelNeff's answer below) has determined that unprefixed attributes do not inherit the namespace of their containing element. Does this mean the XmlSerializer is producing off-spec attributes? Is there a way to force it to add the prefix? It will add a prefix if a different namespace uri is added in the XmlAttribute attribute.
OOPS: My interpretation of spec is wrong. This incorrect response is marked as the answer, so I can't delete it. Sorry.
Attributes without prefixes are in the same namespace as their containing element. You only need to apply a prefix to an attribute if the attribute has a namespace different from its element.
Default namespace declarations do not apply directly to attribute names; the interpretation of unprefixed attributes is determined by the element on which they appear.
http://www.w3.org/TR/2009/REC-xml-names-20091208/#defaulting
Try using the Prefix property?
XmlAttribute Prefix on MSDN
This is a rather odd problem, also setting the Prefix and NamespaceURI manually is probably error prone. Are you sure the namespace on the attribute is even necessary? While it may not be to spec, the client or server you are working with should skip the containing element of the attribute if it is in your foo namespace right? At which point why would it care what namespace your attribute has.
I have an XPath query which looks right to me, but isn't returning any results.
The XML document it's being tested against:
<Deployment xmlns="http://schemas.microsoft.com/client/2007/deployment" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Deployment.Parts>
<AssemblyPart x:Name="foo" Source="foo.dll" />
</Deployment.Parts>
</Deployment>
The code:
Xml = new XmlDocument();
Xml.LoadXml(text);
Manager = new XmlNamespaceManager(Xml.NameTable);
//use constants for namespaces to make more readable
Manager.AddNamespace("a", NS_DEPLOYMENT_2007); //use 'a' for default namespace here so xpath is easier
Manager.AddNamespace("x", NS_XAML_2006);
string xpath="//a:Deployment.Parts/a:AssemblyPart[#a:Source='foo.dll']";
var tmp = Xml.SelectNodes(xpath, Manager);
What is wrong with my XPath query here?
You need to remove the namespace prefix from your attribute:
string xpath="//a:Deployment.Parts/a:AssemblyPart[#Source='foo.dll']";
You only need to specify the namespace for the attribute if it explicitly has a namespace defined, so when you would want to query the Name attribute, you would have to add it:
string xpath="//a:Deployment.Parts/a:AssemblyPart[#x:Name='foo']";
I suspect this part is your problem:
#a:Source='foo.dll'
Unlike element names, attribute names don't inherit a namespace. Your document doesn't specify a namespace for the attribute, so I don't think you should do so either.
Try just:
#Source='foo.dll'
(As an aside, I would personally use LINQ to XML instead of XPath - I find it generally simpler. YMMV, but it may be worth considering - if you're using .NET 3.5 or higher, of course.)
From "Namespaces in XML 1.0 (3rd edition)" section 6.2 (emphasis mine):
The scope of a default namespace declaration extends from the beginning of the start-tag in which it appears to the end of the corresponding end-tag, excluding the scope of any inner default namespace declarations. In the case of an empty tag, the scope is the tag itself.
A default namespace declaration applies to all unprefixed element names within its scope. Default namespace declarations do not apply directly to attribute names; the interpretation of unprefixed attributes is determined by the element on which they appear.
I know this is pretty silly but just wondered if anyone had a link or knows exactly what this code is doing on my page?
namespace com.gvinet.EblAdapter.ebl
{
[Serializable]
[DesignerCategory("code")]
[GeneratedCode("System.Xml", "4.0.30319.225")]
[DebuggerStepThrough]
[XmlType(Namespace = "http://addresshere")]
public class TSAPassenger
{
then here is all of the strings for the form like name, address and such
I am thinking it is trying to grab the XML file that was created from the Database but just want to make sure.
It is not. These are all just metadata attributes.
Serializeable - Use the standard XmlSerializer to take public properties and fields and convert to XML for transport using no customization to the format (like ISerializable would). It is usually only used when going out of process (remoting, Web Services, WCF, etc)
DesignerCategory - This can be used a number of ways. This one tends to be used by the property grid in visual studio as a way to organize sections.
GeneratedCode - The application generated it for you, utilizing the System.Xml namespace in version 4.0.
DebuggerStepThrough - If you are stepping through code (F11), by default, skip over anything here (don't step into a property getting for example).
XmlType - Part of the serializer that allows you to provide a specific namespace that is generated in the output.
The items here do not actually get anything, just describe certain aspects of how something may be loaded/handled.
Hope that makes sense.
The Serializable and XmlType attributes are instructing the XML serializer that the class can be serialized and the schema to use when doing so.
XmlType Attribute
Serializable Attribute
DesignerCategory("code") Attribute
DebuggerStepThrough Attribute
These are attributes - used for declarative programming - you can find more about declarative programming online. But here is the link to .net attribute hierarchy page to get you started: http://msdn.microsoft.com/en-us/library/aa311259(VS.71).aspx
Also, these pages may be helpful:
What are attributes: What are attributes in .NET?
Attributes in C#: http://www.codeproject.com/Articles/2933/Attributes-in-C