2016-04-13 16 views
0

üzerinde desteklenmiyor XML'yi ayrıştırmaya çalışıyorum ve XMLreader kullanarak tüm bilgileri almayı deniyorum. Bir nedenden ötürü başlığı hatayı atıyor. İşte xml geçerli:ReadElementContentAsString yöntemi, node type Text

<?xml version="1.0" encoding="utf-8" ?> 
<queries> 
    <selectparent table="Associates">"SELECT * FROM @table"</selectparent> 
    <selectchild table="VehicleContract">"SELECT * FROM @table"</selectchild> 
    <addchild table="VehicleContract" read="contractId, regNr, assoc_id, percentage, vehicleType, trailerNr">"INSERT into @table (contractId, regNr, assoc_id, percentage, vehicleType, trailerNr) VALUES (@id, @reg, @assort, @perc, @type, @trailer)"</addchild> 
    <updatechild table="VehicleContract" read="contractId, regNr, assoc_id, percentage, vehicleType, trailerNr">"UPDATE @table SET [email protected], [email protected], [email protected], [email protected], [email protected] WHERE [email protected]"</updatechild> 
    <removechild table="VehicleContract" read="id">"DELETE from @table WHERE [email protected]"</removechild> 
</queries> 

Ve yöntemi:

public string selectParent, selectChildren, addChildren, updateChildren, removeChildren; 
public string[] addParams, updateParams, removeParams; 
XmlTextReader reader = new XmlTextReader(base.Path); 
reader.WhitespaceHandling = WhitespaceHandling.None; 
reader.Read(); 
reader.Read(); 
reader.Read(); 
selectParent = reader.ReadElementContentAsString(); 
reader.Read(); 
selectChildren = reader.ReadElementContentAsString(); 
addParams = reader.GetAttribute("read").Split(','); 
addChildren = reader.ReadElementContentAsString(); 
updateParams = reader.GetAttribute("read").Split(','); 
updateChildren = reader.ReadElementContentAsString(); 
removeParams = reader.GetAttribute("read").Split(','); 
removeChildren = reader.ReadElementContentAsString(); 

bu hat üzerinde hata alıyorum: selectParent = reader.ReadElementContentAsString sorunu ne? reader.Read() eklemeye devam ettim çünkü aynı hatayı ancak farklı düğüm tiplerinde tutmaya devam ettim.

Bildiğim kadarıyla, tablo özniteliği sorgudan @table'ın yerini almalı ... ama bir şey doğru değil. Herhangi bir fikir?

+0

kullanmak. Kodunuzu XML olarak bir dize olarak eklediğimde ve yeni MemoryStream (Encoding.UTF8.GetBytes (xml)) 'yi kullanarak okuyucuya yüklediğimde kodunuz benim için çalışır. Ayrıca, kodunuzu ayıklayabilir ve 'reader.NodeType' ve' reader.Name''e bakabilirseniz, bu dosyada nerede olduğunuzu söyleyecektir. –

+0

Tüm yolu kullandım ve hata ayıklayıcısında, beni xml'ye götürür. Bu satıra geldiğimde,> "SELECT * FROM @table" Mocktheduck

cevap

0

Ben Sen `base.Path` beklenen XML dosyasına atıfta olup olmadığını kontrol etmek isteyebilirsiniz xml linq

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

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string xml = 
       "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" + 
       "<queries>" + 
        "<selectparent table=\"Associates\">\"SELECT * FROM @table\"</selectparent>" + 
        "<selectchild table=\"VehicleContract\">\"SELECT * FROM @table\"</selectchild>" + 
        "<addchild table=\"VehicleContract\" read=\"contractId, regNr, assoc_id, percentage, vehicleType, trailerNr\">\"INSERT into @table (contractId, regNr, assoc_id, percentage, vehicleType, trailerNr) VALUES (@id, @reg, @assort, @perc, @type, @trailer)\"</addchild>" + 
        "<updatechild table=\"VehicleContract\" read=\"contractId, regNr, assoc_id, percentage, vehicleType, trailerNr\">\"UPDATE @table SET [email protected], [email protected], [email protected], [email protected], [email protected] WHERE [email protected]\"</updatechild>" + 
        "<removechild table=\"VehicleContract\" read=\"id\">\"DELETE from @table WHERE [email protected]\"</removechild>" + 
       "</queries>"; 

      XDocument doc = XDocument.Parse(xml); 

      var results = doc.Elements("queries").Select(x => new 
      { 
       selectParent = x.Elements("selectparent").Select(y => new 
       { 
        table = (string)y.Attribute("table"), 
        value = y.Value 
       }).FirstOrDefault(), 
       selectChild = x.Elements("selectchild").Select(y => new 
       { 
        table = (string)y.Attribute("table"), 
        value = y.Value 
       }).FirstOrDefault(), 
       addChild = x.Elements("addchild").Select(y => new 
       { 
        table = (string)y.Attribute("table"), 
        read = (string)y.Attribute("read"), 
        value = y.Value 
       }).FirstOrDefault(), 
       updateChild = x.Elements("updatechild").Select(y => new 
       { 
        table = (string)y.Attribute("table"), 
        read = (string)y.Attribute("read"), 
        value = y.Value 
       }).FirstOrDefault(), 
       removeChild = x.Elements("removechild").Select(y => new 
       { 
        table = (string)y.Attribute("table"), 
        read = (string)y.Attribute("read"), 
        value = y.Value 
       }).FirstOrDefault(), 
      }).FirstOrDefault(); 
     } 
    } 
} 
+0

Şu anda herhangi bir türde Linq kullanmam mümkün değil, gerçekten anlamaya çalışıyorum neden kodum çalışmıyor – Mocktheduck

+0

Tüm sonuçları saklamak için bir sınıf oluşturmanız gerekir. Sonra XElement sınıfının linq olarak kabul edilip edilmediğini öğrenirim. XElement sınıfı, XmlElement sınıfının geliştirilmiş bir sürümüdür. – jdweng