|
Guys, give me your thoughts on how you validate your xml at runtime. I recently fell into a drawn-out debug session where I couldn't see the problem with my xml file. I opened it in c#, read it, and checked for various attributes and what have you, adopting plan B that if I don't find an attribute I'll assume a default value.
The problem was I had an attribute called Recursion but I inadvertently spelt it Recursive, not the same thing!
I know I could validate the xml with an xsd file which sounds great in principal but is that the preferred way to validate your xml or do you do it the "safer" (word used very loosely) way by keeping all the literal names in your code and then issue warnings if things don't pass muster?
The xsd file itself could for any number of reasons be subject to an incorrect edit. I'd like to know which way you do things to ensure that when it comes to the usage, your xml is as clean to use as you can make it. Ideas?
If there is one thing more dangerous than getting between a bear and her cubs it's getting between my wife and her chocolate.
|
|
|
|
|
I would say an xsd file is the way to do it. You could possibly verify your xsd file with a CRC32 value.
|
|
|
|
|
First time I have to do this. I know, where have I been the last 10 years...
Given a structure:
struct {
int A;
int B;
another_struct aStruct;
float fValue;
} my_binary_data;
What is the best practice way to dump this in Xml format as well as read it? We're using rapidXml on this project, and what I've seen in existing code is line after line of sprint operations to format and then insert each tagged value into the Xml file. Pretty much the same thing trying to read the Xml back in.
My concern is that some of my structures are going to be changing day by day, and keeping the conversion code up to data is going to be tedious.
Anyone have a better way to do this, or is this just the nature of the beast?
Charlie Gilley
<italic>You're going to tell me what I want to know, or I'm going to beat you to death in your own house.
"Where liberty dwells, there is my country." B. Franklin, 1783
“They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759
|
|
|
|
|
Since XML is text based you have very little choice. Perhaps you need to investigate whether XML is the optimum solution to your problem.
Use the best guess
|
|
|
|
|
Appreciate the feedback. As in many projects, some things are being decided by other people. I'm just trying to get a handle on what to expect. If it has to be done this way, it has to be done this way .
Past projects have exchanged data between devices using binary. This has disadvantages - byte swapping issues, messages tied to data structures, etc.
Charlie Gilley
<italic>You're going to tell me what I want to know, or I'm going to beat you to death in your own house.
"Where liberty dwells, there is my country." B. Franklin, 1783
“They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759
|
|
|
|
|
I am receiving xml from an external web service that I am consuming in a C# application.
The xsd has this line:
<xs:element type="xs:byte" name="contract_term"/>
Most of the files received have this element set to a value but I have just come across one that has it empty:
<contract_term/>
Is this valid, or should I raise it with the web service producer?
|
|
|
|
|
It's syntactically valid XML; what's the problem?
Use the best guess
|
|
|
|
|
I would expect that a value of 0 to 255 would be present for an element marked as byte and it should never be empty. It caused the .NET XmlSerializer to throw an exception when deserializing the data.
I have done a work around by deserializing to a string instead and having an additional property that returns the result parameter of byte.TryParse on the string, but it's a bit messy.
|
|
|
|
|
If you think it's wrong then you should talk to the provider of the XML data.
Use the best guess
|
|
|
|
|
I have raised it with them, but not being very knowledgeable in the finer points of XML/XSD I wasn't sure about it's validity.
|
|
|
|
|
Try making the byte a nullable. As far as I know byte is generally a value type which means it can't be null.
e.g. in c#
byte? elementName
|
|
|
|
|
Yes, you are correct about byte being a value type (in .NET anyway). I haven't tried using nullable so don't know how the XmlSerializer will respond to it - could be interesting!
|
|
|
|
|
I have had a look round a few sites but could not find a definitive statement on this.
Use the best guess
|
|
|
|
|
The dev at the company who provide the web service got back to me saying there should always be a value, and they would of course fix it.
|
|
|
|
|
It is valid -- but, the creator of the XSD did not indicate that it was required -- and you have assumed that it is required. If your code cannot tolerate that -- then modify the XSD to make it required and have whoever produces the XML give it a default value....
However, this is simply pushing it off to the side. This is an issue that does not relate to XML per se -- it relates to best practices about defining and validating data.
The XSD defines the structure and rules of XML -- so the designer of the schema must describe the data perfectly and consumers must not make assumptions that are not supported by the XSD.
|
|
|
|
|
I have a XML which has list items like
<list type="expl" margin="0">
<li label="(1)">
<p num="n">
<text>
(1) A person
is guilty of an offence if:
</text>
</p>
</li>
</list>
<list type="expl" margin="0">
<li label="(a)">
<p num="n">
<text>
(a) the
person deals with money or other property; and
</text>
</p>
</li>
</list>
The output i need is like
<list type="expl">
<li label="(1)">
<p num="n">
<text>A person is guilty of an offence if:</text>
</p>
<list type="expl">
<li label="(a)">
<p num="n">
<text>the person deals with money or other property; and</text>
</p>
</li>
</list>
</list>
Effectively if List appears as consecutive siblings, and they are of different types (eg, (1)/(a)/(i)) they must be nested inside the previous list item.
|
|
|
|
|
<Messages>
<category Name="Main">
<message id="1" title="one" name="Name1" Description="Description1">
</message>
</category>
<category Name="Main1">
<message id="2" title="two" name="Name2" Description="Description2">
</message>
</category>
|
|
|
|
|
|
public static string filePath = HttpContext.Current.Server.MapPath("~/App_Data/Messages.xml");
public static XmlDocument xmlDoc = new XmlDocument();
XmlNode _xmlNode = xmlDoc.CreateElement("category");
XmlAttribute _xmlCategoryName = xmlDoc.CreateAttribute("Name");
_xmlCategoryName.Value = categoryName;
_xmlNode.Attributes.Append(_xmlCategoryName);
// Add Message Node
XmlElement _xmlElement = xmlDoc.CreateElement("message");
XmlAttribute _xmlId = xmlDoc.CreateAttribute("id");
_xmlId.Value = Convert.ToString(ID);
_xmlElement.Attributes.Append(_xmlId);
XmlAttribute _xmltitle = xmlDoc.CreateAttribute("title");
_xmltitle.Value = Convert.ToString(title);
_xmlElement.Attributes.Append(_xmltitle);
XmlAttribute _xmlname = xmlDoc.CreateAttribute("name");
_xmlname.Value = Convert.ToString(name);
_xmlElement.Attributes.Append(_xmlname);
XmlAttribute _xmlDescription = xmlDoc.CreateAttribute("Description");
_xmlDescription.Value = Convert.ToString(description);
_xmlElement.Attributes.Append(_xmlDescription);
_xmlNode.AppendChild(_xmlElement);
xmlDoc.DocumentElement.AppendChild(_xmlNode);
|
|
|
|
|
This is not the document you want for figuring out CRUD. You need the XSD to do that properly. You could guess and as long as the database column names matched you *might* be able to guess correctly, but...I would not be helping you if I recommended that you do that.
If you can write your own query to select a row and if you also had access to ADO.NET -- you could have the table tell you its schema and then the query could be written properly.
This method that you are using -- is not a great idea -- you can have some success in doing it, but not for long and it is very likely that you will manage to put a row in the database that will be junk data because you do not know the schema -- you are just making blind CRUD operations.
A better choice might be stored procedures because you can just provide the parameters from the client side. At any rate, the less you count on being smart on the client side, the better off you will be.
|
|
|
|
|
good morning to everyone,
i want to display some of the questions stored in an xml file on a listbox in wpf,only one questions should be displyed on listbox by using a button,it shows different questions each time whenever i press next button,please send me code for this
thanks in advance
|
|
|
|
|
adinarayana09580 wrote: please send me code for this See point 2[^].
Use the best guess
|
|
|
|
|
Before you read on, this is not about checking for valid XML syntax or schema.
I would like to be able to check if the user is opening an XML file that has data for my application only. If they try to open any other XML file the application should warn them.
The XML file data is serialized via XmlSerializer.
Thanks,
Mark
|
|
|
|
|
Add some kind of identifier for your application, perhaps also including a version number.
|
|
|
|
|
Does such a thing require adding a new namespace?
Mark
|
|
|
|