How to mount XML with C#?

Asked

Viewed 329 times

1

I need to build an XML like in the following example:

<?xml version="1.0" encoding="utf-8"?> 
<MeuXML>
   <Clientes>  
        <Cliente-1>Dados Cliente-1</Cliente>  
   </Clientes> 
   <Sistemas>   
        <Sistema-1>
            <Pasta>Pasta do Sistema-1</Pasta>
            <Origem>Origem do Sistema-1</Fontes>    
        </Sistema-1>
   </Sistemas>
</MeuXMl>

I can even assemble an empty initial XML, but when I add a new client for example (like Client-1), my code duplicates the Clients tag. The same thing happens with systems. I’m doing it this way with systems (example of System-1):

 XElement xml = XElement.Load("Config.xml")
 XElement xmlSistemas = XElement.Load("Config.xml").Elements("Sistemas").FirstOrDefault();
 XElement xSistema1 = new XElement("Sistema-1");
 XElement xPasta = new XElement("Pasta");
 xPasta.Value = "Pasta do Sistema-1";
 XElement xOrigem = new XElement("Origem");
 xOrigem.Value = "Origem do Sistema-1";
 xSistema1.Add(xPasta, xOrigem);
 xmlSistemas.Add(xSistema1);
 xml.Add(xmlSistemas);
 xml.Save("Config.xml");

How can I fix this? Thank you!

  • Like duplica a tag de clientes? Post as is the XML and the code you are using to add a client...

  • The example of the code is for systems tag that is happening the same thing with customers. Inside XML I have the <Meuxml> tag and inside it the <Clients> and <Systems> tags, after I run the code I added to the post the <Systems> tag appears twice, and I need it to appear only once, understood?

1 answer

3


You are adding the tag <Sistemas> (and your children) that you created in the xml file (tag Meuxml), which already has a tag Sistemas - then you get two. What you need to do is add the new system (xSistema1) tag Sistemas that already exists in the file. The code below shows how this can be done.

class Program
{
    const string XML = @"<?xml version=""1.0"" encoding=""utf-8""?> 
<MeuXML>
   <Clientes>  
        <Cliente-1>Dados Cliente-1</Cliente-1>  
   </Clientes> 
   <Sistemas>   
        <Sistema-1>
            <Pasta>Pasta do Sistema-1</Pasta>
            <Origem>Origem do Sistema-1</Origem>    
        </Sistema-1>
   </Sistemas>
</MeuXML>";

    static void Main(string[] args)
    {
        XElement xml = XElement.Parse(XML);
        XElement xmlSistemas = xml.Elements("Sistemas").FirstOrDefault();
        XElement xSistema2 = new XElement("Sistema-2");
        XElement xPasta = new XElement("Pasta");
        xPasta.Value = "Pasta do Sistema-2";
        XElement xOrigem = new XElement("Origem");
        xOrigem.Value = "Origem do Sistema-2";
        xSistema2.Add(xPasta, xOrigem);
        xmlSistemas.Add(xSistema2);
        Console.WriteLine(xml);
    }
}
  • But how do I save to a file? Type, after xmlSistemas.Add(xSistema2); I should add the xml.Save line("Config.xml")?

  • 1

    Yes, you can save it back. This one is just a complete example (http://answall.com/help/mcve) illustrating the solution to the problem.

Browser other questions tagged

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