SORU
4 AĞUSTOS 2009, Salı


XDocument.() Olabilirdi XML Kodlama Etiketi damla

Xml olabilirdi kodlama almak için herhangi bir yolu() Fonksiyonu?

Örnek:

xml.Save("myfile.xml");

yol açar

<?xml version="1.0" encoding="utf-8"?>
<Cooperations>
  <Cooperation>
    <CooperationId>xxx</CooperationId>
    <CooperationName>Allianz Konzern</CooperationName>
    <LogicalCustomers>

Ama

tb_output.Text = xml.toString();

böyle bir çıkış yol açar

<Cooperations>
  <Cooperation>
    <CooperationId>xxx</CooperationId>
    <CooperationName>Allianz Konzern</CooperationName>
    <LogicalCustomers>
    ...

CEVAP
4 AĞUSTOS 2009, Salı


Ya açıkça beyan yazmak veya StringWriter kullanma ve Save() Ara:

using System;
using System.IO;
using System.Text;
using System.Xml.Linq;

class Test
{
    static void Main()
    {
        string xml = @"<?xml version='1.0' encoding='utf-8'?>
<Cooperations>
  <Cooperation />
</Cooperations>";

        XDocument doc = XDocument.Parse(xml);
        StringBuilder builder = new StringBuilder();
        using (TextWriter writer = new StringWriter(builder))
        {
            doc.Save(writer);
        }
        Console.WriteLine(builder);
    }

Kolayca bir uzantısı yöntemi olarak ekleyebilirsiniz:

public static string ToStringWithDeclaration(this XDocument doc)
{
    if (doc == null)
    {
        throw new ArgumentNullException("doc");
    }
    StringBuilder builder = new StringBuilder();
    using (TextWriter writer = new StringWriter(builder))
    {
        doc.Save(writer);
    }
    return builder.ToString();
}

Bu patlama eğer oraya gitmez avantajı vardırdeğilbeyan :)

Sonra da kullanabilirsiniz:

string x = doc.ToStringWithDeclaration();

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Chip Johnson

    Chip Johnson

    30 AĞUSTOS 2007
  • Dumb Stupid Videos

    Dumb Stupid

    26 Kasım 2013
  • Jimmie Jones

    Jimmie Jones

    16 Kasım 2007