2015-12-22 120 views
0

我需要以下代碼的虛擬XML,有if (reader.NodeType == //XmlNodeType.Element && reader.Name.ToLower() == "transmissions") condition so如何創建虛擬XML結構?

 public BusinessEntities.FileParseResult Parse(string FilePath) 
    { 

     FileParseResult _fileParseResult = new FileParseResult(); 
     _fileParseResult.FileParseResultType = FileParseResultType.ValidFileClaim; 


     Logger.LogMessage(PREPROCESS_FILEPARSER.TRACE_MSG, "****WorkComp.Net : Pre Processor -> Start parsing valid AT XML File *****"); 
     XmlReaderSettings settings = new XmlReaderSettings(); 

     settings.ConformanceLevel = ConformanceLevel.Fragment; 
     using (XmlReader reader = XmlReader.Create(FilePath, settings)) 
     { 
      try 
      { 
       string tranmissionsattr = string.Empty; 
       string header = string.Empty; 
       while (reader.Read()) 
       { 
        if (reader.NodeType == XmlNodeType.Element && reader.Name.ToLower() == "transmissions") 
        { 
         for (int i = 0; i < reader.AttributeCount; i++) 
         { 
          reader.MoveToAttribute(i); 
          tranmissionsattr = tranmissionsattr + " " + reader.Name + "=\"" + reader.GetAttribute(i) + "\""; 
         } 
         reader.MoveToElement(); 
        } 
       } 
      } 
     } 
    } 
+0

'<傳輸/>'? –

回答

0

什麼

<transmissions attribute1="test1" attribute2="test2" /> 
<transmissions attribute1="test1" attribute2="test2" /> 
<transmissions attribute1="test1" attribute2="test2" /> 
<transmissions attribute1="test1" attribute2="test2" /> 

:)

+0

如果傳輸是根標籤,則不起作用。你只能有一個根標籤。 – jdweng

+0

@jdweng如果一致性級別設置爲fragment(它是),那麼它將正常工作。每[文檔](https://msdn.microsoft.com/en-us/library/system.xml.xmlwritersettings.conformancelevel%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396)*「This設置接受在頂層具有多個根元素或文本節點的XML數據。「* –

0

下面是一些測試代碼

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

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     const string FILENAME = @"c:\temp\test.xml"; 
     static void Main(string[] args) 
     { 
      BusinessEntities be = new BusinessEntities(); 
      be.Parse(FILENAME); 
     } 
    } 
    public class BusinessEntities 
    { 
     public enum FileParseResultType 
     { 
      ValidFileClaim 
     } 
     public class FileParseResult 
     { 
      public FileParseResultType FileParseResultType { get; set; } 
     } 
     public BusinessEntities.FileParseResult Parse(string FilePath) 
     { 

      FileParseResult _fileParseResult = new FileParseResult(); 
      _fileParseResult.FileParseResultType = FileParseResultType.ValidFileClaim; 


      //Logger.LogMessage(PREPROCESS_FILEPARSER.TRACE_MSG, "****WorkComp.Net : Pre Processor -> Start parsing valid AT XML File *****"); 
      XmlReaderSettings settings = new XmlReaderSettings(); 

      settings.ConformanceLevel = ConformanceLevel.Fragment; 
      string xml = "<?xml version=\"1.0\" ?><transmissions version=\"1.0\" xmlns=\"workcomp.net/wcn2/public\"><transmission><header></header><reports><report><R4>FL</R4><R5></R5>JCN<R15>ClaimTest1</R15></report></reports><trailer recordcount=\"1\"/></transmission></transmissions>"; 
      StringReader sreader = new StringReader(xml); 
      Console.WriteLine(xml.Substring(195,10)); 
      using (XmlReader reader = XmlReader.Create(sreader, settings)) 
      { 
       try 
       { 
        string tranmissionsattr = string.Empty; 
        string header = string.Empty; 
        while (reader.Read()) 
        { 
         if (reader.NodeType == XmlNodeType.Element && reader.Name.ToLower() == "transmissions") 
         { 
          for (int i = 0; i < reader.AttributeCount; i++) 
          { 
           reader.MoveToAttribute(i); 
           tranmissionsattr = tranmissionsattr + " " + reader.Name + "=\"" + reader.GetAttribute(i) + "\""; 
          } 
          reader.MoveToElement(); 
         } 
        } 
       } 

       catch (Exception e) 
       { 
        string error = e.Message; 
       } 
      } 
      return _fileParseResult; 
     } 
    } 
} 
​ 

​ 
+0

感謝您的迴應。 string xml =「<?xml version = \」1.0 \「?>

FL JCN ClaimTest1<拖車的RecordCount = \ 「1 \」/>「; 問題是: 使用(的XmlReader讀卡器= XmlReader.Create(XML,設置)) 時,我的調試器觸及到上面一行它給我一個錯誤的路徑 非法字符。 我無法提供XML的物理路徑。 – Chetan

+0

結束標記具有不可見的非法字符。複製並粘貼此評論中的標籤,該標籤沒有不可見的字符。 – jdweng

+0

我編輯了我的答案,現在包括你的xml字符串。 – jdweng