Click here to Skip to main content
16,020,257 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All,

I have a XML file which have a node called Section as below. As XML treats & and < sign as an invalid characters. So i want to replace < sign with 'AmpersandLTsemicolon ('<')' and & with 'AmpersandAMPsemicolon' in Section Node as shown below.


I want 'AmpersandLTsemicolon' should be replaced only in section node.

below is my XML markup :-

C#
<?xml version="1.0" encoding="utf-8"?>
    <webpage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Tabs>
      <Tab Name="Description">
         <Section Name="Description < &">
            <Field Type="Hidden" Property="EmployeeID" Desc="Employee ID" ColWidth="20"/>
         </Section>
     </Tab>
    </Tabs>
</Webpage>


New XML should be :-

C#
<?xml version="1.0" encoding="utf-8"?>
    <webpage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Tabs>
      <Tab Name="Description">
         <Section Name="Description < &"> like AmpersandLTSemicolon and AmpersandAMPSemicolon
            <Field Type="Hidden" Property="EmployeeID" Desc="Employee ID" ColWidth="20"/>
         </Section>
     </Tab>
    </Tabs>
    </Webpage>


Please help me on this.

What I have tried:

I tried to replace < with AmpersandLTSemicolon using indexOF method, then it replaces AmpersandLTSemicolon in all start tags of XML file.
Posted
Comments
Suvendu Shekhar Giri 21-Feb-18 4:49am    
Please show the code you already have tried.
Member 11052432 21-Feb-18 5:02am    
if (xmlString.indexOf("&") >= 0 || xmlString.indexOf("<") >= 0)
{
xmlString = xmlString.replace(/\&/g, '&').replace(/</g, '<');
} -- like AmpersandLTsemicolon and AmpersandAMPSemicolon... AmpersandLTsemicolon is not showing in code project as it is denied like it should be
replace(/</g, 'AmpersandLTSemicolon in HTML form').

Kindly solve this issue.
A_Griffin 21-Feb-18 5:14am    
What's wrong with
Replace("<Section Name=""Description < &"">", "<Section Name=""Description [whatever you want]"">")
Member 11052432 21-Feb-18 5:32am    
< sign does not work in XML. When is use $.parseXML method, then it shows invalid XML. That is the reason i need to replace less than sign with AmpersandLTSemicolon to parse it a valid XML.

How to find out Section node or name and find less than sign inside it and replace only in Section node.
A_Griffin 21-Feb-18 5:40am    
Yes, I know... so replace it as I suggested.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900