|
|
I'm using an XML file to store user preferences, but I can't decide on what's the best format. The data would easily been stored in an INI file using sections, keys, and values, but I want to use XML instead. I'm come up with a couple of alternatives:
1.
<?xml version="1.0" encoding="utf-8"?>
<profile>
<section name="Some Section">
<key name="Some Key">Some Value</key>
<key name="Some Other Key">Another Value</key>
</section>
<section name="Some Other Section">
...
</section>
</profile>
2.
<?xml version="1.0" encoding="utf-8"?>
<profile>
<Some_Section>
<Some_Key>Some Value</Some_Key>
<Some_Other_Key>Another Value</Some_Other_Key>
</Some_Section>
<Some_Other_Section>
...
</Some_Other_Section>
</profile>
Which of these two would you choose and why? If you know of a better alternative, I'd love to hear it.
Thanks!
Alvaro
|
|
|
|
|
Out of those two, the first one. Easier to validate, also it's probably easier to enumerate/navigate via XPath, if that were an issue.
Generally, your XML element names should be drawn from a small set of fixed names, which is met by the first example.
HTH
Stuart Dootson
'Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p'
|
|
|
|
|
Yep, I was leaning toward the first one as well, although I originally wrote the code with the second format in mind.
I'm just now barely getting my feet wet with XPath. I used it to retrieve the values for the second format using "section_name/key_name", which seemed pretty simple. For the first format, where attributes are involved, what's the proper XPath string I need? Also if you could provide a link or two that quickly gets me up to speed on the XPath basics, I'd appreciate it very much.
Thanks Stuart!
Regards,
Alvaro
Can I ask you a question?
|
|
|
|
|
To get a particular keys value, you'd use something like '//section[@name='section-name]/key[@name='key-name']/text()' - hmmm, doesn't look any easier, does it. I guess you're probably best going with what you've got - after all, you're not intending to exchange the data with any other application, are you?
The reason I tend to use the first form you presented was because I tend to parse the whole file using libxml2 - the platform I'm limited by doesn't have .NET or an up-to-date MSXML
I'm not sure what the best starters guides to XPath are - I learnt it through learning XSLT from Michael Kay's XSLT reference book (published by Wrox).
Stuart Dootson
'Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p'
|
|
|
|
|
Hi,
I would strongly recommend that you get your basics right with XSL and XPath before jumping into the structure design, as knowing them even at a basic level would give you a clear idea about which is the easiest and efficient way to access and maintain your data.
I liked the following two articles from devshed, through which i learned abt those techs:
XSL - http://www.devshed.com/Server_Side/XML/XSLBasics/XSLBasics1/page1.html
XPath - http://www.devshed.com/Server_Side/XML/XPath/page1.html
All the best.
CreProDes
|
|
|
|
|
the choice depends on what you are trying to do with the xml.
if you want a very generic solution then the first is better.
however, if you want to have a very specific solution the second is better - for example by have different elements for different keys you can set up your xsd to be tighter...
also the second example is easier to understand (from a human point of view)
either way its going to be a {} war... so choose the one your prefer
"When the only tool you have is a hammer, a sore thumb you will have."
|
|
|
|
|
Code along the lines of:
string value1 = currentNode.Attributes["value1"].Value;
However, if the Node does not contain an attribute named "value1", and exception would be thrown.
So, is there a way (without kludging with try..catch blocks) to determine if a given attribute exists for a node?
TIA
~Mike Stanbrook
|
|
|
|
|
IF NOT currentNode.Attributes.ItemOf("value1") IS NOTHING THEN
value1 = currentNode.Attributes.Item("value1")
END IF
|
|
|
|
|
There is an easier way by using XPath Queries.
...asuming you have an XMLDocument Open
Dim xNode As Xml.XmlNode = xDoc.SelectSingleNode("//Element[@Attribute='Value']")
If (Not IsNothing(xNode)) Then
'Do Something
End If
|
|
|
|
|
Are you sure this is easier/more efficient that the method previously suggested, especially given that the current element node is already known and sotred in a local variable?
~Mike Stanbrook
|
|
|
|
|
Hi ,
I want to create one xml document from a dataset where in some fields I am having null values.
can any one help me out?
thanks
|
|
|
|
|
Hey,
If you dont have values, xml generated from the dataset will be having blank values.
Let me know is this your problem ?
Regards
Sri
|
|
|
|
|
Hi ,
I have solved my problem .Actually if i am passing null values in dataset then it is not creating the xml element .
So instead of passing null i passed null string("").Then it worked fine for me .
Thanks
|
|
|
|
|
Hi,
I have solved my problem.
Actually if I am passing null values in dataset xml is not creating the xml element.
so instead of passing null I passed null string("").
After that it worked for me.
thanks
|
|
|
|
|
If u no of a site that can save me time, please send me the link.
I was wondering if u goes have an example on how I should build a lookup table for countries.
For a dropdownlist:
CountryName = "USA"
Value = 0
CountryName="Canada"
Value = 1
Should I change below the value to a an integer instead of text.
Say I want to build a lookup for sql server
Here is an example?
<countries>
<country>
<text>Canada
<value>Canada
<country>
<text>Afghanistan
<value>Afghanistan
<country>
<text>Albania
<value>Albania
<country>
<text>Algeria
<value>Algeria
<country>
<text>American Samoa
<value>American Samoa
<country>
<text>Andorra
<value>Andorra
<country>
<text>Angola
<value>Angola
<country>
<text>Anguilla
<value>Anguilla
<country>
<text>Antarctica
<value>Antarctica
<country>
<text>Antigua and Barbuda
<value>Antigua and Barbuda
<country>
<text>Argentina
<value>Argentina
<country>
<text>Armenia
<value>Armenia
<country>
<text>Aruba
<value>Aruba
<country>
<text>Australia
<value>Australia
<country>
<text>Austria
<value>Austria
<country>
<text>Azerbaijan
<value>Azerbaijan
<country>
<text>Bahrain
<value>Bahrain
<country>
<text>Bangladesh
<value>Bangladesh
<country>
<text>Barbados
<value>Barbados
<country>
<text>Belarus
<value>Belarus
<country>
<text>Belgium
<value>Belgium
<country>
<text>Belize
<value>Belize
<country>
<text>Benin
<value>Benin
<country>
<text>Bermuda
<value>Bermuda
<country>
<text>Bhutan
<value>Bhutan
<country>
<text>Bolivia
<value>Bolivia
<country>
<text>Bosnia and Herzegovina
<value>Bosnia and Herzegovina
<country>
<text>Botswana
<value>Botswana
<country>
<text>Bouvet Island
<value>Bouvet Island
<country>
<text>Brazil
<value>Brazil
<country>
<text>British Indian Ocean Territory
<value>British Indian Ocean Territory
<country>
<text>Brunei Darussalam
<value>Brunei Darussalam
<country>
<text>Bulgaria
<value>Bulgaria
<country>
<text>Burkina Faso
<value>Burkina Faso
<country>
<text>Burundi
<value>Burundi
<country>
<text>Cambodia
<value>Cambodia
<country>
<text>Cameroon
<value>Cameroon
<country>
<text>Cape Verde
<value>Cape Verde
...
|
|
|
|
|
I need something that correctly 'cleans' a string that exists in xml by removing all the & and other tags that translate into regular characters...
thanks
|
|
|
|
|
If you are using .NET and the DOM (or an System.Xml.XmlTextWriter) then this is handled automatically for you. If you are just creating xml by manipulating strings or cutting and pasting then you will need a utility to handle it for you.
If this was written in vb.NET the following class would handle it for you.
<br />
Public Class XMLUtility<br />
Private Sub New()<br />
End Sub<br />
Public Shared Function ReplaceIllegalXMLChars(ByVal s As String) As String<br />
s = s.Replace("&", "&")<br />
s = s.Replace(">", ">")<br />
s = s.Replace("<", "<")<br />
s = s.Replace(ControlChars.Quote, """)<br />
s = s.Replace("'", "'")<br />
Return s<br />
End Function<br />
End Class<br />
It would be a simple task to turn it into a simple app to paste a string into a text box click convert and display it in another text box. If you wanted me to knock something up then drop me a line.
Another way to do it would be using an html page with javascript to perform the same task in exactly the same manner.
Jim
|
|
|
|
|
Hi Jim,
What I have is C/C++ code that has the XML page contained in a buffer.
The best thing for me would be to use a function like the one you described here. Are you positive these are all the special cases I may encounter ?
is ControlChars.Quote the equivalent for C's \" ?
Thanks
|
|
|
|
|
Just to confirm the characters: & < > " '
ControlChars.Quote is the equivalent for C's \"
Jim
|
|
|
|
|
I need something that correctly 'cleans' a string that exists in xml by removing all the & and other tags that translate into regular characters...
thanks
|
|
|
|
|
Your XML parser will almost certainly already do this for you. The specific answer depends on what you are using to parse the XML in the first place?
--
-Blake (com/bcdev/blake)
|
|
|
|
|
at first I tried it myself, but obviously this left many tags hanging around.
So I moved to a library called 'PugXML' I found here on FlipCode but from my test-code it didn't remove the &-style-tags... I'm still waiting for a reply from the author.
I'd be happiest If I could see the exeact rules (should there not be too many) and write the cleanup code myself...
Or if there's such a 'cleanup-code' available somewhere I'd be glad to use it
thanks for the help
|
|
|
|
|
Hi. I'm very new in .NET.
I need to
1. display data from a fixed width text file to datagrid
2. do some validation and update these data to database.
I'm looking to convert fixed width text file to XML file to do the above. Have search a lot of website and can't seem to find any sample that explain converting a FIXED width file to XML, except converting delimited to XML.
Appreciate advise and help from everyone.
|
|
|
|
|
Pseudocode (language independant):
1 Load the string into a variable.
2 Split into an array (split on the carraige return + line feed).
3 Create a root node
[XML Doc looks like this now <Rows/>]
4 Loop through the array and create a node to contain the row.
[XML Doc looks like this now <Rows><Row/></Rows>]
5 Extract each part of the string in the array (using the fixed width to define which part of the string you want) and create a node with the text and append it to the row node.
[XML Doc looks like this now <Rows><Row><Column1>ExtractedTextChars0-10</Column1><Column2>ExtractedTextChars11-20</Column2><Column3>ExtractedTextChars21-30</Column3></Row><Row><Column1>ExtractedTextChars0-10</Column1><Column2>ExtractedTextChars11-20</Column2><Column3>ExtractedTextChars21-30</Column3></Row><Row><Column1>ExtractedTextChars0-10</Column1><Column2>ExtractedTextChars11-20</Column2><Column3>ExtractedTextChars21-30</Column3></Row></Rows>]
I could provide some VB.Net code to do this if it would help.
Jim
|
|
|
|