Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hey experts can any one give an idea of how to insert cdata and make all child nodes in small letters in side following code of text to xml conversion.
C#
string content = File.ReadAllText(@"wpassetd.txt");

Regex expression = new Regex(@"##(?<parent>\w+)\r\n(?<children>[\w ]+)\r\n((?<values>[\S ]+)(\r\n(?!#)|$))+", RegexOptions.None);
XElement xeRoot = new XElement("wpassetd");   //root node
foreach (Match m in expression.Matches(content))
{
    XElement xeParent = new XElement(m.Groups["parent"].Value);

    string[] children = m.Groups["children"].Value.Split(' ');      // Use space as delimter for the children

    foreach (Capture cap in m.Groups["values"].Captures)
    {
        XElement xeChild = new XElement("data");   // Change the name 'child' to whatever suitable
        string[] values = cap.Value.Split(new string[] { "@*@" }, StringSplitOptions.None);

        // Check that the children and values counts are equal
        if (children.Length != values.Length)
            throw new Exception("The number of children and values mismatch.");

        for (int i = 0; i < children.Length; i++)
        {
            XElement xeChildValue = new XElement(children[i]);
            xeChildValue.Value = values[i];
            xeChild.Add(xeChildValue);
        }

        xeParent.Add(xeChild);
    }

    xeRoot.Add(xeParent);
}

XDocument doc = new XDocument();
doc.Add(xeRoot);
doc.Save(@"test.xml");
Posted
Updated 31-Mar-15 3:15am
v2

To get the node names in small letters use String.ToLower()
For example:
C#
XElement xeChildValue = new XElement(children[i].ToLower());



[UPDATE] Thanks to Richard Deeming, who saw the error in my code.
To insert CDATA you should use the class XCData.
Example:
C#
XCData cdata = new XCData(values[i]);
xeChildValue.Add(cdata);


To insert CDATA you can use String.Format()
For example:
C#
xeChildValue.Value = String.Format("<![CDATA[{0}]]>", values[i]);


CDATA - (Unparsed) Character Data[^]

And maybe checkout the various string functions available: String Methods[^]
 
Share this answer
 
v4
Comments
Sergey Alexandrovich Kryukov 31-Mar-15 11:25am    
Agree, a 5. It worth noting that CDATA is not a part of XML logical structure. This is just an alternative way of entering the content for XML element text node.
—SA
Richard Deeming 1-Apr-15 9:07am    
String.Format won't work for inserting a <![CDATA[...]]> section - the Value will be encoded, so you'll end up with &lt;![CDATA[...]]&gt; instead.
George Jonsson 1-Apr-15 10:24am    
That is not correct. I think you think of using CDATA in a DataSet and then use DataSet.WriteXml(). Then you get that result.
With XDocument and XElement it works like a charm.
See my updated answer.
Richard Deeming 1-Apr-15 10:26am    
Did you try it?

var node = new XElement("x");
node.Value = string.Format("<![CDATA[{0}]]>", "hello");
Console.WriteLine(node);


Output:
<x>&lt;![CDATA[hello]]&gt;</x>
George Jonsson 1-Apr-15 10:30am    
Yes I did, but I opened the resulting XML in Internet Explorer and then it shows up as it should.
Now when I looked at the content in Notepad+ I see that you are absolutely right.
Hmm.
XDocument xmldoc = new XDocument();
           xmldoc.Add(xeRoot);
           xmldoc.Save(@"test.xml");
           string text = File.ReadAllText(@"test.xml");
           text = text.Replace("!", "<!");
           text = text.Replace("]]", "]]>");
           File.WriteAllText(@"test.xml", text);
 
Share this answer
 
Comments
Cpmunster 13-Sep-18 5:10am    
Why would you ever suggest a manual inject replacemethod in a codepage issue?
- That is so wrong on so many levels.
- Its the sole reason to why this world has so many codepage conversion problems. - There are so many XML tools out there.

Your file format is destroyed if you use a replacemethod.

Think about it!
When other developers use their tools, reads the fileheader that says maybe utf8. But your replacemethod rewrites the file to a .NET format (iso-88something), which is also called ANSI. On top of that, you are ALSO _adding_ the risk of using symbols not permitted by the codepage in the first place.

Do you realise that actual people come by here and read your input?

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