|
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
|
|
|
|
|
Jim,
I've been trying to learn VB.NET... If you could provide some code to do this it would be greatly appreciated!
In advance,
Thanks
|
|
|
|
|
Console application:
<br />
Option Strict Off<br />
Imports System.IO<br />
Imports System.Xml<br />
Module Module1<br />
<br />
Sub Main()<br />
Try<br />
Dim iStartPositions As Integer() = {0, 4, 5, 14}<br />
Dim sFixedWidthText As String = GetFileContents("C:\FixedWidthText.txt")<br />
Dim sw As New StringWriter<br />
Dim writer As New XmlTextWriter(sw)<br />
writer.WriteStartElement("Rows")<br />
Dim sRows As String() = sFixedWidthText.Split(vbCrLf)<br />
Dim iRow, iColumn As Integer<br />
Dim sRowText As String<br />
Dim al As ArrayList<br />
For iRow = 0 To sRows.Length - 1<br />
writer.WriteStartElement("Row")<br />
sRowText = sRows(iRow)<br />
al = GetDelimitedTextArray(sRowText, iStartPositions)<br />
For iColumn = 0 To al.Count - 1<br />
writer.WriteStartElement("Column" & iColumn)<br />
writer.WriteString(al(iColumn))<br />
writer.WriteEndElement()<br />
Next<br />
writer.WriteEndElement()<br />
Next<br />
writer.WriteEndElement()<br />
SaveTextToFile(sw.ToString, "C:\FixedWidthText.xml")<br />
Console.WriteLine("File saved")<br />
Console.ReadLine()<br />
Catch ex As Exception<br />
Console.WriteLine(ex.ToString)<br />
Console.ReadLine()<br />
End Try<br />
End Sub<br />
Public Function GetDelimitedTextArray(ByVal sRowText As String, ByVal iStartPositions As Integer()) As ArrayList<br />
Dim al As New ArrayList<br />
Dim i As Integer<br />
Dim sColumnText As String<br />
For i = 0 To iStartPositions.Length - 1<br />
If i = iStartPositions.Length - 1 Then<br />
sColumnText = sRowText.Substring(iStartPositions(i))<br />
Else<br />
sColumnText = sRowText.Substring(iStartPositions(i), iStartPositions(i + 1) - iStartPositions(i))<br />
End If<br />
al.Add(sColumnText)<br />
Next<br />
Return al<br />
End Function<br />
Public Function GetFileContents(ByVal FullPath As String) As String<br />
Dim sContents As String<br />
Dim oReader As New StreamReader(FullPath)<br />
sContents = oReader.ReadToEnd()<br />
oReader.Close()<br />
Return sContents<br />
End Function<br />
Public Sub SaveTextToFile(ByVal sData As String, ByVal FullPath As String)<br />
Dim oWriter As StreamWriter<br />
oWriter = New StreamWriter(FullPath)<br />
oWriter.Write(sData)<br />
oWriter.Close()<br />
End Sub<br />
<br />
End Module<br />
<br />
<br />
Contents of test file:
0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
Hope this helps. Will need to improve exception handling and data checks (i.e. If row text is less than expected etc). Also best to encapsulate functionality as a seperate utility class with overloaded functions i.e. to get xml as string, save as file or return xmldom document etc.
Jim
|
|
|
|
|
This is probably an easy one...
I have implemented a CHOOSE function in an XSLT transformation as follows:
<code>
<!-- ====[CONTENT]================================ -->
<xsl:variable name="selBoxHeight" select="count(./Content/content_name)"></xsl:variable>
<TR><TD>
<DIV><xsl:value-of select="$selBoxHeight" /></DIV>
<!-- HTML ListBox -->
<SELECT>
<xsl:attribute name="class">menuoptionselectboxes</xsl:attribute>
<!-- Set Select Box height -->
<xsl:choose>
<xsl:when test="$selBoxHeight <= 7">
<xsl:attribute name="Size"><xsl:value-of select="$selBoxHeight"></xsl:value-of></xsl:attribute>
</xsl:when>
<xsl:otherwise>
<xsl:attribute name="Size"><xsl:value-of select="7"></xsl:value-of></xsl:attribute>
</xsl:otherwise>
</xsl:choose>
<code>
I'm having a problem with the evaluation of :
test="$selBoxHeight <=7" where the transformation fails, returning the following error:
System.Xml.XmlException: '<', hexadecimal value 0x3C, is an invalid attribute character. Line 51
I have verified that this sort of comparison can be performed, see for example: O'Reilly's XML.Com.
Even so , I have this niggling error.
Has anyone got help on the matter??
Thanks in advance
Drikus
|
|
|
|
|
test="$selBoxHeight <=7"
|
|
|
|
|
|
Hello!
I program with Configuration Management application block
I need to use configuration files at more then two levels:
<Section>
<SubSection>
<SubSection>
<Element>
</SubSection>
</SubSection>
How can I do it using CMAB? ConfigurationSectionHandler?
</Section>
|
|
|
|