使用c#在xml中的cdata [英] cdata in xml using c#

查看:132
本文介绍了使用c#在xml中的cdata的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,专家可以任意一个人知道如何插入cdata并将所有子节点放在文字代码旁边的小写字母中进行xml转换。

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.

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");

推荐答案

))+,RegexOptions.None);
XElement xeRoot = new XElement( wpassetd); / / root node
foreach (匹配m in expression.Matches(content))
{
XElement xeParent = new XElement(m.Groups [ parent]。Value);

string [] children = m.Groups [ children]。值。拆分(' '); // 使用空格作为子项的分隔符

foreach (捕获上限 in m.Groups [ values]。捕获)
{
XElement xeChild = new XElement( 数据); // 将名称child更改为任何合适的
string [] values = cap.Value.Split( new string [] { @ * @},StringSplitOptions.None);

// 检查子项和值计数是否相等
if (children.Length!= values.Length)
throw new 异常( 子项数和值不匹配。 );

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);
))+", 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");


要以小写字母获取节点名称,请使用 String.ToLower()

例如:

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







[更新]感谢Richard Deeming,他在我的代码中看到了错误。

要插入CDATA,您应该使用类XCData。

示例:




[UPDATE] Thanks to Richard Deeming, who saw the error in my code.
To insert CDATA you should use the class XCData.
Example:

XCData cdata = new XCData(values[i]);
xeChildValue.Add(cdata);





要插入CDATA,您可以使用 String .Format()

例如:



To insert CDATA you can use String.Format()
For example:

xeChildValue.Value = String.Format("<![CDATA[{0}]]>", values[i]);





CDATA - (未分析)字符数据 [ ^ ]



也许可以查看各种可用的字符串函数:字符串方法 [ ^ ]


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);


这篇关于使用c#在xml中的cdata的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆