Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi,

 

Is there a simple and efficient way to create an XML from a segmented string?

For example, I would like to convert the following string (is there a better name for this kind of strings?):

 

SEG_ROOT_START*1234~
SEG_LVL1*A*B~
SEG_LVL2*8~
SEG_LVL1*X*~
SEG_ROOT_END~

into:

<SEG_ROOT>
  <SEG_LVL1>
     <SEG_LVL1_01>A</SEG_LVL1_01>
     <SEG_LVL1_02>B</SEG_LVL1_02>
     <SEG_LVL2>
        <SEG_LVL2_01>8</SEG_LVL2_01>
     </SEG_LVL2>
  </SEG_LVL1>
  <SEG_LVL1>
     <SEG_LVL1_01>X</SEG_LVL1_01>
     <SEG_LVL1_01 />
  </SEG_LVL1>
</SEG_ROOT>

 

In this example,

  • The root segment has two forms, one for begin, one for end. We need to parse this specially
  • There are two levels, LVL1 (expecting two string childrens) and LVL2 (expecting one integer child)
  • Level LVL2 can only be children of LVL1
  • When an expected value shows an empty string, the corresponding XML node shouuld be NULL.

 

Currently, we are reading and parsing these strings manually. I am wondering if there are existing ways (regular expressions, maybe?) so that we can convert them into XMLs.

 

We can do the following with the XMLs:

  1. Data validation once XSD is defined.
  2. Data transformation once XSLT is defined.
  3. Data transmission via web services.

 

Thanks a lot for any information or suggestions,

Alan.

Posted

If you are processing them manually, as long as you are using the StringBuilder class you're probably not going to get much better performance than that.

 

There may be an elegent way to use regular expressions, but I'll have to defer to someone else.

~Nate�

___________________________________________

Please use the [vb]/[cs] tags on posted code.

Please post solutions you find somewhere else.

Follow me on Twitter here.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...