|
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>
|
|
|
|
|
I'm just at the early stages of learning XML, which no specific target at the moment. When I'm learning new technologies, one tactic I like to employ is to take an old application of mine and "upgrade" to use the new technology. That way I'm starting off in familiar territory and doing incremental learning. That's my theory, anyway.
So anyway, I have my somewhat aging shareware app which uses MFC's CDocument & CArchive for database storage. It looks to me to be an ideal application for conversion to XML storage.
My question: before I get too far down the wrong path, is there an easy way to initially port my MFC code to use XML storage instead of CArchive, or should I just use one of the sample classes posted here and rewrite all of my serialization code?
I'd have hoped MS would have updated MFC to include a "CXMLArchive" class to drop in place, but apparently that was a bit too much to hope for. :-/
|
|
|
|
|
I am writing a facility in vb6 to export a number of records to an xml file that will then be sent to an organisation for processing. In the example of how they want the xml document to look they use ...
<?xml version="1.0" encoding="UTF-16"?>
However, when I go to view my document in MS Internet Explorer(6.0), I receive the following error ...
The XML page cannot be displayed
Cannot view XML input using style sheet. Please correct the error and then click the Refresh button, or try again later.
Switch from current encoding to specified encoding not supported. Error processing resource 'file:///G:/Temp/104749-031001.xml'. Line 1, Position 40
Removing the encoding let's me view the document correctly, so is the UTF encoding absolutely necessary and does it affect the document if it's used on another machine or os?
Matthew
Live to ride. Ride to live.
|
|
|
|
|
The encoding attribute in the XML processing instruction is to indicate the encoding of the document, so it is likely that the document you are creating is not in that encoding hence the error. To check this look at the xml file you are creating in a hex editor, in utf-16 there are (at least) two bytes per character...
|
|
|
|
|
|
Hello,
Greetings!
I'm trying to make a sample app in VC++ 6.0 to prepare and send a SOAP message to a webserver. I'm using gSOAP Toolkit 2.3 (http://www.cs.fsu.edu/~engelen/soap.html[^]) for the same at the client end, while I have "Windows 2000 Pro/Apache v1.3/PHP-4.3/NuSOAP.php" on the test server end. (Yeah, it is not a Win2K Server, but it is a Professional version of Win2K!)
Any idea about how to make and use the namespace file at the client end?
The documentation (UserGuide) is not quite helpful!! ... So if you have used a similar kind of setup, and can reply back, I would be grateful.
Thanks,
Rgds,
Nirav
---------------------------------------------------
* Don't wish it was easier; Wish you were better! *
---------------------------------------------------
|
|
|
|
|
Hi,
I got the solution with some trial and error...
Thanks anyway!
If anybody needs help on this, please let me know!
Thanks,
Rgds,
Nirav
---------------------------------------------------
* Don't wish it was easier; Wish you were better! *
---------------------------------------------------
|
|
|
|
|
I create dynamic xml document with dom.The xml seem
<record>
<a>fasfd</a>
<b>fdasfas</b>
<record>
or
<record>
<b>fasf</b>
<a>fdsfs</a>
</record>
or
<record>
<a>fds</a>
<a>fsfsfsfd</a>
<b>232432</b>
<a>werwrw</a>
<record>
how do i specify the order? the complexType must is: sequence or choice or all,but these don't apply my xml doc. Have there a way that don't need specify order?
linjie
|
|
|
|
|
Use a repeating choice ordering group, eg for you examples
<complexType>
<choice maxOccurs="unbounded">
<element name="a" type="string"/>
<element name="b" type="string"/>
</choice>
</complexType>
|
|
|
|
|
Thanks very muth,My problem has be solved.
linjie
|
|
|
|
|
1.I create a xml document with dom. I want specify a schema to the xml document? how do i do?
2.The xml document created by dom isn't a formated document,e.g
<record>sdafs</record><record>fsdfsdfa</record>..............
are there a way to format it for newline and indenting?
linjie
|
|
|
|
|
I hope this provides some clarity:
Create a schema for your example document (snippet), which I formatted as follows:
<?xml version="1.0" encoding="UTF-8"?>
<ROOT xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="C:\...\record.xsd">
<record>sdafs</record>
<record>fsdfsdfa</record>
</ROOT>
The schema reference has been inserted as a noNamespaceSchemaLocation, in this example the XSD schema located somewhere on your local drive, eg c:\.
The schema, which I created with XMLSpy, may look something like this:
<?xml version="1.0" encoding="UTF-8"?>
<!--W3C Schema generated by XMLSPY v2004 rel. 2 U (http:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="ROOT">
<xs:complexType>
<xs:sequence>
<xs:element name="record">
<xs:complexType/>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Validating the XML document will now happen against the XSD schema. An XSLT transform may be applied to render it in a browser.
|
|
|
|