2013-02-19 13 views
6

Mam plik xml, który muszę aktualizować za każdym razem, zgodnie z wymogiem nowego klienta. Przez większość czasu xml nie jest odpowiedni z powodu ręcznego uaktualniania pliku xml. Mam na myśli napisanie programu (web/windows), w którym zapewniona jest poprawna weryfikacja. i na podstawie danych wejściowych z interfejsu ui utworzę plik xml. poniżej jest mój przykładowy plik xml.generowanie plików xml na podstawie moich C# klas

<community> 
    <author>xxx xxx</author> 
    <communityid>000</communityid> 
    <name>name of the community</name> 

<addresses> 
     <registeredaddress> 
      <addressline1>xxx</addressline1> 
      <addressline2>xxx</addressline2> 
      <addressline3>xxx</addressline3> 
      <city>xxx</city> 
      <county>xx</county> 
      <postcode>0000-000</postcode> 
      <country>xxx</country> 
     </registeredaddress> 
     <tradingaddress> 
      <addressline1>xxx</addressline1> 
      <addressline2>xxx</addressline2> 
      <addressline3>xxx</addressline3> 
      <city>xxx</city> 
      <county>xx</county> 
      <postcode>0000-000</postcode> 
      <country>xxx</country> 
     </tradingaddress> 
     </addresses> 


<community> 

Czy ktoś może mi pomóc, jaki będzie najlepszy sposób na to?

+1

Jakiego rodzaju sprawdzania poprawności mówisz? –

+0

jak dla wyrażenia regularnego pola pocztowego – Prashant

+0

@Prashant czy ważne jest, aby znaczniki xml były małe? –

Odpowiedz

11

Tworzenie następujących klas trzymać swoje dane i zatwierdź go:

public class Community 
{ 
    public string Author { get; set; } 
    public int CommunityId { get; set; } 
    public string Name { get; set; } 
    [XmlArray] 
    [XmlArrayItem(typeof(RegisteredAddress))] 
    [XmlArrayItem(typeof(TradingAddress))] 
    public List<Address> Addresses { get; set; } 
} 

public class Address 
{ 
    private string _postCode; 

    public string AddressLine1 { get; set; } 
    public string AddressLine2 { get; set; } 
    public string AddressLine3 { get; set; } 
    public string City { get; set; } 
    public string Country { get; set; } 

    public string PostCode 
    { 
     get { return _postCode; } 
     set { 
      // validate post code e.g. with RegEx 
      _postCode = value; 
     } 
    } 
} 

public class RegisteredAddress : Address { } 
public class TradingAddress : Address { } 

I serializacji tę instancję klasy społecznej do xml:

Community community = new Community { 
    Author = "xxx xxx", 
    CommunityId = 0, 
    Name = "name of community", 
    Addresses = new List<Address> { 
     new RegisteredAddress { 
      AddressLine1 = "xxx", 
      AddressLine2 = "xxx", 
      AddressLine3 = "xxx", 
      City = "xx", 
      Country = "xxxx", 
      PostCode = "0000-00" 
     }, 
     new TradingAddress { 
      AddressLine1 = "zz", 
      AddressLine2 = "xxx" 
     } 
    } 
}; 

XmlSerializer serializer = new XmlSerializer(typeof(Community)); 
serializer.Serialize(File.Create("file.xml"), community); 

myślę trochę googling pomoże Ci zrozumieć, jak deserializować obiekt społecznościowy z powrotem z pliku.

+0

Znaczniki xml w języku xml są takie same jak w plikach CS. jak mogę zrobić na mniejsze etui, jak w moim oryginalnym pliku xml. – Prashant

+0

dzięki za szybki soln. – Prashant

+1

@Prashant użyj atrybutów 'XmlElement'. W ten sposób '[XmlElement (" nazwa ")] public string Name {get; zestaw; } 'również będziesz potrzebować atrybutu' XmlRoot' dla klasy społeczności. –

Powiązane problemy