2016-04-05 14 views
-1

Aşağıdaki XML'yi arka planda C# kodunda json olarak ayrıştırmam gerekiyor.XML Verilerini JSON'a Dönüştürme

<rss version="2.0"> 
<channel> 
<title>TITLE </title> 
<link>http://sample.com/</link> 
<language>en-Us</language> 
<pubDate>System.String[] GMT</pubDate> 
<item> 
<ParentSection>News</ParentSection> 
<ParentSectionTamilname>செய்திகள்</ParentSectionTamilname> 
<ParentSectionID>1</ParentSectionID> 
<Count>6</Count> 
<items> 
<Section>National</Section> 
<Sectiontamil>தேசியச்செய்திகள்</Sectiontamil> 
<Sectionid>3</Sectionid> 
<SectionURL> 
http://sample.com/rss/RssfeedXML.aspx?Id=3&Main=1 
</SectionURL> 
<SectionJsonURL> 
http://sample.com/json/JsonfeedXML.aspx?Id=3&Main=1 
</SectionJsonURL> 
<subitem> 
<Sub_Count>0</Sub_Count> 
</subitem> 
</items> 
<items> 
<Section>National</Section> 
<Sectiontamil>தேசியச்செய்திகள்</Sectiontamil> 
<Sectionid>3</Sectionid> 
<SectionURL> 
http://sample.com/rss/RssfeedXML.aspx?Id=3&Main=1 
</SectionURL> 
<SectionJsonURL> 
http://sample.com/json/JsonfeedXML.aspx?Id=3&Main=1 
</SectionJsonURL> 
<subitem> 
<Sub_Count>0</Sub_Count> 
</subitem> 
</items> 
</item> 

<item> 
<ParentSection>News</ParentSection> 
<ParentSectionTamilname>செய்திகள்</ParentSectionTamilname> 
<ParentSectionID>1</ParentSectionID> 
<Count>6</Count> 
<items> 
<Section>National</Section> 
<Sectiontamil>தேசியச்செய்திகள்</Sectiontamil> 
<Sectionid>3</Sectionid> 
<SectionURL> 
http://sample.com/rss/RssfeedXML.aspx?Id=3&Main=1 
</SectionURL> 
<SectionJsonURL> 
http://sample.com/json/JsonfeedXML.aspx?Id=3&Main=1 
</SectionJsonURL> 
<subitem> 
<Sub_Count>0</Sub_Count> 
</subitem> 
</items> 
<items> 
<Section>National</Section> 
<Sectiontamil>தேசியச்செய்திகள்</Sectiontamil> 
<Sectionid>3</Sectionid> 
<SectionURL> 
http://sample.com/rss/RssfeedXML.aspx?Id=3&Main=1 
</SectionURL> 
<SectionJsonURL> 
http://sample.com/json/JsonfeedXML.aspx?Id=3&Main=1 
</SectionJsonURL> 
<subitem> 
<Sub_Count>0</Sub_Count> 
</subitem> 
</items> 
</item> 
</channel> 
</rss> 

Aşağıda XML'i JSON'a dönüştürmek için kullandığım C# kodum var, ancak başarısız oluyor. SerializeObject kullanarak serileştiremiyorum.

var xml = new XmlDocument(); 
       xml.LoadXml(ABOVE XMLSTRING); 
       string jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(xml, Newtonsoft.Json.Formatting.None); 

Bu herhangi bir Yardım çok takdir edilecektir ..

teşekkür ederiz.

+1

Bu Json serializer'ı bilmiyorum, ama sanırım doğrudan bir "XmlDocument" örneğini buraya yerleştiremezsiniz !? Daha çok, girdi olarak özellik ve değerlerle somut bir sınıf örneği beklediği gibi görünüyor. –

+0

Yanıtınız için teşekkür ederiz Kim – Anbarasi

+1

JsonConvert.SerializeXmlNode (xml) kullanmayı deneyin – Kevin

cevap

4

. Sen nesne seri değil SerializeXmlNode yöntemini çağırmanız gerekir

<?xml version="1.0" encoding="utf-8" ?> 
<rss version="2.0"> 
    <channel> 
    <title>TITLE </title> 
    <link>http://sample.com/</link> 
    <language>en-Us</language> 
    <pubDate>System.String[] GMT</pubDate> 
    <item> 
     <ParentSection>News</ParentSection> 
     <ParentSectionTamilname>செய்திகள்</ParentSectionTamilname> 
     <ParentSectionID>1</ParentSectionID> 
     <Count>6</Count> 
     <items> 
      <Section>National</Section> 
      <Sectiontamil>தேசியச்செய்திகள்</Sectiontamil> 
      <Sectionid>3</Sectionid> 
      <SectionURL> 
       http://sample.com/rss/RssfeedXML.aspx?Id=3&amp;Main=1 
      </SectionURL> 
      <SectionJsonURL> 
       http://sample.com/json/JsonfeedXML.aspx?Id=3&amp;Main=1 
      </SectionJsonURL> 
      <subitem> 
       <Sub_Count>0</Sub_Count> 
      </subitem> 
     </items> 
     <items> 
      <Section>National</Section> 
      <Sectiontamil>தேசியச்செய்திகள்</Sectiontamil> 
      <Sectionid>3</Sectionid> 
      <SectionURL> 
       http://sample.com/rss/RssfeedXML.aspx?Id=3&amp;Main=1 
      </SectionURL> 
      <SectionJsonURL> 
       http://sample.com/json/JsonfeedXML.aspx?Id=3&amp;Main=1 
      </SectionJsonURL> 
      <subitem> 
       <Sub_Count>0</Sub_Count> 
      </subitem> 
     </items> 
    </item> 

    <item> 
     <ParentSection>News</ParentSection> 
     <ParentSectionTamilname>செய்திகள்</ParentSectionTamilname> 
     <ParentSectionID>1</ParentSectionID> 
     <Count>6</Count> 
     <items> 
      <Section>National</Section> 
      <Sectiontamil>தேசியச்செய்திகள்</Sectiontamil> 
      <Sectionid>3</Sectionid> 
      <SectionURL> 
       http://sample.com/rss/RssfeedXML.aspx?Id=3&amp;Main=1 
      </SectionURL> 
      <SectionJsonURL> 
       http://sample.com/json/JsonfeedXML.aspx?Id=3&amp;Main=1 
      </SectionJsonURL> 
      <subitem> 
       <Sub_Count>0</Sub_Count> 
      </subitem> 
     </items> 
     <items> 
      <Section>National</Section> 
      <Sectiontamil>தேசியச்செய்திகள்</Sectiontamil> 
      <Sectionid>3</Sectionid> 
      <SectionURL> 
       http://sample.com/rss/RssfeedXML.aspx?Id=3&amp;Main=1 
      </SectionURL> 
      <SectionJsonURL> 
       http://sample.com/json/JsonfeedXML.aspx?Id=3&amp;Main=1 
      </SectionJsonURL> 
      <subitem> 
       <Sub_Count>0</Sub_Count> 
      </subitem> 
     </items> 
    </item> 
</channel> 

altına

&amp; // Use instead of & 

biçimlendirilmiş xml dizedir. SerializeObject, xml - C# örneğini dönüştürmek için kullanılır.

İşte kullanarak json için xml seri hale getirmek kod newtonsoft

XmlDocument xmlDoc = new XmlDocument(); 
    xmlDoc.Load("InputXml.xml"); // Can use xmlDoc.LoadXml(YourString); 
    string jsonText = JsonConvert.SerializeXmlNode(xmlDoc); 

kod sakar yapar dize olarak bir dosya ziyade sabit koddan bir dosya ve yük içinde xml kaydetmek için iyi bir uygulamadır.

1

Önce xml'nizi düzeltmeniz gerekiyor. Onu ayrıştırmak mümkün değil. Hataları bulmak için VS kullanın. Ayrıca, düzeltmeniz gereken URL adreslerinin özel karakterleri de vardır.

4

JSON XML seri hale getirmek yerine JsonConvert.SerializeXmlNode() kullanmış olmalıdır:

Sen xml standartlarına uygun olarak xml dizesi biçimlendirmek gerekir
var xml = new XmlDocument(); 
xml.LoadXml("your XML here"); 
string jsonString = Newtonsoft.Json.JsonConvert.SerializeXmlNode(xml); 
İlgili konular