XML format enforcement

G

Guest

I wish to write some kind of XML format enforcer like DTD, XSD or other that
can do the following:
(1) Any element and node must have Type attribute that can be read as data
type by the parser.
(1) Any element and node may have (optional) four more attributes.

I XML enforcer should allow any tree structure and any number of nodes in
any tree.

Does anyone can tell how it's possible?

Any example will be most appreciated.
 
R

robert

Sharon,
It certainly can be done. Visual Studio 2005 can use XSD, but not DTD;
has methods for attaching external xsd's or you can place the xsd
inline in the code. Check out the VS documentation. w3.org has
specifications etc.
Robert
 
G

Guest

Thanks Robert for your reply.

I know that XSD can be attached to an XML file, but I don't know how the SAD
should look like in order to enforce the formatting I have posted.

Can you post en example showing this?

P.S.: I'm using VS 2003.
 
G

Guest

It seems that ant DTD or XSD defining the node name (parameter name in my
case), and this not good for me.

I need to let the user enter any node name he likes, but I do want to force
him to use my predefined attributes.
And I can't not find a way to do that.

Is there ?
 
G

Guest

Ok, I think I found a way to do thanks to Marc Clifton articale on
CodeProject (http://www.codeproject.com/dotnet/MycroXaml.asp).

The XML file can be somthing like that:

<?xml version="1.0" encoding="utf-8"?>
<System Name="System">
<Paramaters>
<Speed type="System.Iint32">111</Speed >
<_xAxis type="System.Single">222</_xAxis>
<_yAxis type="System.Double">333</_yAxis>
<_ID type="System.Int64">444</_ID>
<_Name type="System.String">String value...</_Name>
</Paramaters>
</System>


And the C# code can be:

System.IO.StreamReader sr = new System.IO.StreamReader("TypesDef.xml");
string text = sr.ReadToEnd();
sr.Close();
XmlDocument doc = new XmlDocument();
doc.LoadXml(text);
XmlNode node = doc.FirstChild;
while( node.NodeType.ToString() != "Element" )
{
node = node.NextSibling;
}

XmlNode paramsNode = doc.SelectSingleNode("//Paramaters");
XmlNodeList nodeList = paramsNode.ChildNodes;
Hashtable verTypes = new Hashtable(nodeList.Count);
string typeName = "";
Type type;

foreach( XmlNode xmlNode in nodeList )
{
XmlAttribute attr = xmlNode.Attributes["type"];
typeName = attr.Value;
if( typeName == "System.String" )
{
verTypes[xmlNode.Name] = xmlNode.InnerText;
}
else
{
type = Type.GetType(typeName, false);
object typeIntance = Activator.CreateInstance(type);
System.Reflection.MethodInfo Parse = type.GetMethod("Parse", new Type
[] {typeof(String)});
verTypes[xmlNode.Name] = Parse.Invoke(type, new object[]
{xmlNode.InnerText});
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top