c# - Replace all elements name and attrbiutes that contains hyphens with underscore -


i have following xml example:

<root-name file="something" date="22.02.2015" target="name" full-name="something else">    <root-child title ="title" published-by="fullname" published-by-userid="username">        <root-doc id="123" owner-id="username" />        <root-doc id="1234" owner-id="username" />        <root-doc id="12345" owner-id="username" />    </root-child>     <root-child title ="title" published-by="fullname" published-by-userid="username">        <root-doc id="abc" owner-id="username" />        <root-doc id="abcd" owner-id="username" />        <root-doc id="abcde" owner-id="username" />    </root-child> ..................     </root-name> 

how can replace elements names , attributes contain hyphen underscore?

example:

<root-name file="something" date="22.02.2015" target="name" full-name="something else"> 

should be

<root_name file="something" date="22.02.2015" target="name" full_name="something else">   

attributes names little trickier element names because attribute names not have setter. thus:

        var doc = xdocument.parse(xml);          foreach (var element in doc.descendants())         {             if (element.name.localname.contains("-"))             {                 var newname = element.name.localname.replace('-', '_');                 element.name = element.name.namespace + newname;             }             var list = element.attributes().tolist();             (int = 0; < list.count; i++)             {                 var attr = list[i];                 if (attr.name.localname.contains("-"))                 {                     xattribute newattr = new xattribute(attr.name.namespace + attr.name.localname.replace('-', '_'), attr.value);                     list[i] = newattr;                 }             }             element.replaceattributes(list);         } 

Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -