2012-07-07 95 views
-1

我正在執行我的項目中的XML讀取過程。在哪裏我必須閱讀XML文件的內容。我已經實現了它。如何在不使用XML屬性的情況下讀取包含XML元素的字符串

出於好奇,我也嘗試使用相同的方法,將XML內容保留在字符串中,然後只讀取elemet標記中的值。即使這我已經實現了。以下是我的代碼。

string xml = <Login-Form> 
       <User-Authentication> 
        <username>Vikneshwar</username> 
        <password>xxx</password> 
       </User-Authentication> 

       <User-Info> 
        <firstname>Vikneshwar</firstname> 
        <lastname>S</lastname> 
        <email>[email protected]</email> 
       </User-Info> 
      </Login-Form>"; 
     XDocument document = XDocument.Parse(xml); 

var block = from file in document.Descendants("client-authentication") 
      select new 
      { 
       Username = file.Element("username").Value, 
       Password = file.Element("password").Value, 
      }; 

foreach (var file in block) 
{ 
    Console.WriteLine(file.Username); 
    Console.WriteLine(file.Password); 
} 

同樣,我獲得了我的另一組元素(名字,姓氏和電子郵件)。現在我的好奇心再次吸引我。現在我正在考慮使用字符串函數來做同樣的事情?

在上面的代碼中使用相同的字符串將採取。我試圖不使用任何與XMl相關的類,即XDocumentXmlReader等。只應使用字符串函數來實現相同的輸出。我無法做到這一點。可能嗎?

+5

是的,它是可能的,但做的不是一個推薦或標準的方式。 – 2012-07-07 07:12:04

+0

只是給我一些提示來做到這一點。我沒有太多熟悉字符串functuions。 – Vikneshwar 2012-07-07 07:17:26

+0

您的XML是否包含特殊(轉義)字符,名稱空間,cdata?你要求的是以或多或少的方式重建XElement.Parse()。忘掉它。 – 2012-07-07 07:20:23

回答

1

可以毫不正則表達式來完成,像這樣:

string[] elementNames = new string[]{ "<username>", "<password>"}; 
foreach (string elementName in elementNames) 
{ 
    int startingIndex = xml.IndexOf(elementName); 
    string value = xml.Substring(startingIndex + elementName.Length, 
     xml.IndexOf(elementName.Insert(1, "/")) 
     - (startingIndex + elementName.Length)); 
    Console.WriteLine(value); 
} 

用正則表達式:

string[] elementNames2 = new string[]{ "<username>", "<password>"}; 
foreach (string elementName in elementNames2) 
{ 
    string value = Regex.Match(xml, String.Concat(elementName, "(.*)", 
     elementName.Insert(1, "/"))).Groups[1].Value; 
    Console.WriteLine(value); 
} 

當然,唯一推薦的就是使用XML解析類

+0

這真的很棒。謝謝你,伊萬。 – Vikneshwar 2012-07-07 08:15:07

+0

Ivan我使用了xml類。只是想知道是否有其他選項可用。 Ur的帖子給了我很多幫助。非常感謝。 – Vikneshwar 2012-07-07 08:18:11

+0

不客氣:) – 2012-07-07 08:32:44

2

你可以使用像IndexOf, Equals, Substring等在String類提供的方法來滿足您的需求,更多信息Go here

使用Regex是一個相當大的選擇了。

但爲此目的,建議使用XmlDocument類。

+0

實際上,既然['XDocument'](http://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument.aspx)存在,我會*不推薦使用舊的和麻煩的'XMLDocument'。 [Jon Skeet同意](http://stackoverflow.com/a/1542101/1106367)。 – Adam 2012-07-07 08:53:55

4

不要這樣做。複雜的規則包括嵌套,字符轉義,命名實體,命名空間,排序(屬性與元素),註釋,未分析的字符數據和空白等。例如,只需添加

<!-- 
    <username>evil</username> 
--> 

或者

<parent xmlns=this:is-not/the/data/you/expected"> 
    <username>evil</username> 
</parent> 

或者,也許在CDATA部分是相同的 - 看看基本的基於字符串的方法是如何很好地工作。提示:您將通過DOM獲得不同的答案。

使用專用工具設計爲用於讀取XML是正確的方法。至少,使用XmlReader - 但坦率地說,一個DOM(比如你現有的代碼)更方便。或者,使用序列化程序(如XmlSerializer)填充對象模型,並查詢

試圖正確解析XML和類似XML的數據不會很好....RegEx match open tags except XHTML self-contained tags

1

建立一個擴展方法,將獲得的標籤之間的文字是這樣的:

public static class StringExtension 
{ 
    public static string Between(this string content, string start, string end) 
    { 
     int startIndex = content.IndexOf(start) + start.Length; 
     int endIndex = content.IndexOf(end) - (end.Length - 1); 
     string result = content.Substring(startIndex, endIndex); 
     return result; 
    } 
} 
相關問題