C# Function convert string to xml

Asked

Viewed 109 times

1

I wonder how to convert a string with XML inline for a XML with tags, I’m using the XmlDocument, I have the following code:

var MihaString="<MeuXML Info01="teste 0121245" Info02="2020-01-14" Info03="2019-12-30"/>";
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(MihaString);
var result = xmlDocument.OuterXml;

However even using Outerxml it maintains the same input xml structure.

Expected result would be something like this:

<MeuXMLPai> 
 <MeuXML>
  <Info01> teste 0121245 </Info01>
  <Info02> 2020-01-14 </Info02>
  <Info03> 2019-12-30 </Info03>
 </MeuXML> 
</MeuXMLPai>
  • I don’t understand your doubt?

  • <Meuxmlpai> <Meuxml> <Info01> test 0121245 </Info01> <Info02> 2020-01-14 </Info02> <Info03> 2019-12-30 </Info03> </Meuxmlpai> </Meuxml>

  • Wanted to transform this xml:<Meuxmlpai> <Meuxml> <Info01> 0121245 test </Info01> <Info02> 2020-01-14 </Info02> <Info03> 2019-12-30 </Info03> </Meuxmlpai> </Meuxml>

1 answer

2


Basically, another needs to be created XmlDocument and create the attributes of that previous for children of the new:

var MihaString = "<MeuXML Info01=\"teste 0121245\" 
                          Info02=\"2020-01-14\" Info03=\"2019-12-30\"/>";
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(MihaString);
           
// a partir daqui é um novo arquivo.
XmlDocument xmlResult = new XmlDocument();

var root = xmlResult.CreateElement("MeuXMLPai");
var items = xmlResult.CreateElement("MeuXML");

foreach(XmlAttribute xmlAttribute in xmlDocument.DocumentElement.Attributes)
{
    var item = xmlResult.CreateElement(xmlAttribute.Name);
    item.InnerText = xmlAttribute.Value;
    items.AppendChild(item);
}
root.AppendChild(items);
xmlResult.AppendChild(root);
Console.WriteLine(xmlResult.OuterXml);

Online example: https://dotnetfiddle.net/wcn1uh

  • I understood, but my string is dynamic, I can’t know what will be my elements, as for example I can receive an XML like this: "<Meuxmlpai> <Meuxml Info01="test 0121245" Info02="2020-01-14" Info03="2019-12-30"> <Itema Codigo="89" Descripcion="" /> <Itemb Code="100" Information="Testing" /> <Itemc Test="Right" Warning="No" Error="No" /> </Meuxml> </Meuxmlpai>"

  • @Luizlanza we help here according to what is in the question, and now this comment is another doubt, if you prefer open another question, but, I believe that has already solved the initial doubt. Meaning you posted a problem and it was solved.

  • Did not answer. I will edit the question to be clearer.

  • You cannot edit the question @Luizlanza to satisfy new doubt!

  • It is not a new question, what you answered there does not meet because of these 2 items: var root = xmlResult.Createelement("Meuxmlpai"); var items = xmlResult.Createelement("Meuxml"); have already edited the question, to make clear that it would be dynamic this.

  • I will reaffirm your initial doubt has been answered according to what was in the question, I will reverse and if you want to create a new question with a more appropriate text, after all this is the rule here and neither you nor I can do anything. Thank you, if you would like to point out in response, feel free!

  • Did you make that much comment, asking me to open another question just because you wanted me to mark your answer? I thought you wanted to help me.

  • Virgilio Novic, ta ai a nova pergunta https://answall.com/questions/458398/c-converter-string-din%C3%a2mica-em-xml-com-tag

  • @Luizlanza was not I who started with the comments and other, help everyone help the problem is the other side of the coin you want to learn from the help? well let there already opened another question that is important, now can not change the original question because you think that now did not work.

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.