2017-01-14 3 views
0

に名前空間を追加します。Osoba、テストC#のXMLシリアルIは、2つのクラスを持っているノード

public class test 
{ 

    public string raz { get; set; } 
    public string dwa { get; set; } 
} 

public class Osoba 
{ 
    public test tehe { get; set; } 
} 

はまた、私は

<?xml version="1.0" encoding="utf-8"?> 
<Osoba xmlns:dfs="http://schemas.microsoft.com/office/infopath/2003/dataFormSolution"xmlns:pc="http://schemas.microsoft.com/office/infopath/2007/PartnerControls"xmlns:d="http://schemas.microsoft.com/office/infopath/2009/WSSList/dataFields"> 
<tehe> 
    <raz>dfdfdfdf</raz> 
    <dwa>fgfg</dwa> 
</tehe> 
</Osoba> 

を取得し、メインルートに名前空間を追加し、

 Osoba ne = new Osoba(); 
     test t1 = new praca2.test(); 


     t1.dwa = "fgfg"; 
     t1.raz = "dfdfdfdf"; 
     ne.tehe = t1;    
     XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); 
     ns.Add("dfs", "http://schemas.microsoft.com/office/infopath/2003/dataFormSolution"); 
     ns.Add("d", "http://schemas.microsoft.com/office/infopath/2009/WSSList/dataFields"); 
     ns.Add("pc", "http://schemas.microsoft.com/office/infopath/2007/PartnerControls"); 

     XmlSerializer xsSubmit = new XmlSerializer(typeof(Osoba)); 
     var xml = @"D:\dupa1.xml"; 

     using (var stream = new FileStream(xml, FileMode.Create)) 
     { 
      using (XmlWriter writer = XmlWriter.Create(stream)) 
      { 
       xsSubmit.Serialize(writer, ne,ns); 
       xml = stream.ToString(); // Your XML 
      } 
     } 

をseralize私はノードの名前空間に追加したいと思う:

... 
    <pc:tehe> 
    <dfs:raz>dfdfdfdf</dfs:raz> 
    <dfs:dwa>fgfg</dfs:dwa> 
    </pc:tehe> 

どうすればいいですか? は私が名前空間

[XmlRoot("Node", Namespace="http://flibble")] 

設定クラスatributeを追加しようとしますが、私は悪い考え

答えて

0

あなたは少しだけ自分のクラスを変更する必要がほとんどそこにいる:

public class test 
    { 
     [XmlElement(Namespace = "http://schemas.microsoft.com/office/infopath/2003/dataFormSolution")] 
     public string raz { get; set; } 
     [XmlElement(Namespace = "http://schemas.microsoft.com/office/infopath/2003/dataFormSolution")] 
     public string dwa { get; set; } 
    } 

    public class Osoba 
    { 
     [XmlElement(Namespace = "http://schemas.microsoft.com/office/infopath/2007/PartnerControls")] 
     public test tehe { get; set; } 
    } 
ほとんどはあなたからコピー

サンプル実装:

class Program 
    { 
     static void Main(string[] args) 
     { 

      Osoba ne = new Osoba(); 
      test t1 = new test(); 

      t1.dwa = "fgfg"; 
      t1.raz = "dfdfdfdf"; 
      ne.tehe = t1; 

      XmlSerializer xsSubmit = new XmlSerializer(typeof(Osoba)); 

      XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); 
      ns.Add("dfs", "http://schemas.microsoft.com/office/infopath/2003/dataFormSolution"); 
      ns.Add("pc", "http://schemas.microsoft.com/office/infopath/2007/PartnerControls"); 
      var xml = @"D:\dupa1.xml"; 


      using (var stream = new FileStream(xml, FileMode.Create)) 
      { 
       using (XmlTextWriter writer = new XmlTextWriter(stream, Encoding.UTF8)) 
       { 
        xsSubmit.Serialize(writer, ne, ns); 
       } 
      } 

     } 
    } 

あなたはこのXMLを取得します:

<?xml version="1.0" encoding="utf-8"?> 
<Osoba xmlns:dfs="http://schemas.microsoft.com/office/infopath/2003/dataFormSolution" xmlns:pc="http://schemas.microsoft.com/office/infopath/2007/PartnerControls"> 
    <pc:tehe> 
     <dfs:raz>dfdfdfdf</dfs:raz> 
     <dfs:dwa>fgfg</dfs:dwa> 
    </pc:tehe> 
</Osoba> 

第H ...

+0

ありがとう!!できます – pepeb333

-1

を使用すると、C#.Iで新しいいると思うあなたはあなたの条件pattern.For C#の基礎、デザインパターンとリポジトリを勉強することをお勧めしますコードの下に使用することができます

public class Tehe 
{ 
    public string Raz { get; set; } 
    public string Dwa { get; set; } 
} 

public class TeheRepository 
{ 
    private System.Xml.Linq.XDocument xmlDatas; 

    public TeheRepository(string filePath) 
    { 
     xmlDatas = XDocument.Load(filePath); 
    } 

    public IEnumerable<Tehe> FindAll() 
    { 
     return xmlDatas.Elements().Elements().Select(tehe => 
     { 
      Tehe t = new Tehe(); 
      t.Dwa = tehe.Elements().ElementAt(1).Value; 
      t.Raz = tehe.Elements().ElementAt(0).Value; 
      return t; 
     }); 
    } 
} 
+0

をこのコードは 'namespaces'と' prefixes'でOPを手助けしますか? – EdSF

+0

TeheオブジェクトをXMLファイルから作成したくありません – Mehmet

0

UisngたXML LINQ:すべてどのよう

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Xml; 
using System.Xml.Linq; 

namespace ConsoleApplication42 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string xmlHeader = 
       "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + 
       "<Osoba xmlns:dfs=\"http://schemas.microsoft.com/office/infopath/2003/dataFormSolution\"" + 
        " xmlns:pc=\"http://schemas.microsoft.com/office/infopath/2007/PartnerControls\"" + 
        " xmlns:d=\"http://schemas.microsoft.com/office/infopath/2009/WSSList/dataFields\">" + 
       "</Osoba>"; 

      XDocument doc = XDocument.Parse(xmlHeader); 

      XElement osoba = doc.Root; 
      XNamespace dfsNs = osoba.GetNamespaceOfPrefix("dfs"); 
      XNamespace pcNs = osoba.GetNamespaceOfPrefix("pc"); 

      osoba.Add(new XElement(pcNs + "tehe", new object[] { 
       new XElement(dfsNs + "raz", "dfdfdfdf"), 
       new XElement(dfsNs + "dwa", "fgfg") 
      })); 
     } 
    } 
} 
関連する問題