2011-10-31 212 views
0

我有類似下面的XML文件:解析XML數據XPath中

<Adresses> 
    <Address1> 
     <XPath>//Address/Local[001]/AddressLine1</XPath> 
     <Key>1</Key> 
    </Address1> 
    <Address2> 
     <XPath>//Address/Local[002]/AddressLine1</XPath> 
     <Key>2</Key> 
    </Address2> 
    <Address3> 
     <XPath>//Address/Local[003]/AddressLine1</XPath> 
     <Key>3</Key> 
    </Address3> 
    <Address4> 
     <XPath>//Address/Local[004]/AddressLine1</XPath> 
     <Key>4</Key> 
    </Address4> 
    <Address5> 
     <XPath>//Address/Local[005]/AddressLine1</XPath> 
     <Key>5</Key> 
    </Address5> 
</Adresses> 

我想使用XPath在C#來解析的XML值。在這種情況下,我想用元素<XPath>上的本地[001]替換本地[002],本地[003],本地[004]和本地[005]。

+0

你試圖完成什麼*部分是造成困難? –

+0

你想用什麼替換它們? – Reddog

+0

我想用Local [001]替換Local [002],Local [003],Local [004]和Local [005]。在發佈XML時也犯了一個錯誤。在Local [00X]部分之後,它應該分別是/ AddressLine1,/ AddressLine2,/ AddressLine3,/ AddressLine4和/ AddressLine5,它們將保持相同。 @Reddog – ncoder

回答

0

您可以使用LINQ to XML輕鬆完成此操作。

這裏是一個非常簡單的例子我扔在一起:

var xml = @"<Adresses> 
    <Address1> 
     <XPath>//Address/Local[00X]/AddressLine1</XPath> 
     <Key>1</Key> 
    </Address1> 
    <Address2> 
     <XPath>//Address/Local[00X]/AddressLine2</XPath> 
     <Key>2</Key> 
    </Address2> 
    <Address3> 
     <XPath>//Address/Local[00X]/AddressLine3</XPath> 
     <Key>3</Key> 
    </Address3> 
    <Address4> 
     <XPath>//Address/Local[00X]/AddressLine4</XPath> 
     <Key>4</Key> 
    </Address4> 
    <Address5> 
     <XPath>//Address/Local[00X]/AddressLine5</XPath> 
     <Key>5</Key> 
    </Address5> 
</Adresses> "; 
Console.WriteLine(xml); 
var xDoc = XDocument.Parse(xml); 
foreach(var elem in xDoc.Descendants("XPath")){ 
    var key = Convert.ToInt32((elem.NextNode as XElement).Value); 
    elem.Value = string.Format("//Address/Local[{0}]/AddressLine{1}",key.ToString("000"),key); 
} 
Console.WriteLine(xDoc.ToString()); 
+0

Acually,中的XPath元素值diffreent除了本地部分。這是我誠實的錯誤。我不想在這個元素中取代整條線。只想用Local [001]更新Local [00X]部分。謝謝@ brendan。 – ncoder

+0

不知道我可以按照您的確切問題,更新的答案應該接近您正在尋找的。 – brendan

0

你的要求還不太清楚。 AFAIK,XPATH只會帶你到目前爲止,因爲它只用於瀏覽你的XML文檔。你可以用它來幫助你找到你想要的元素,但你需要別的東西去做實際的替換。

使用LINQ to XML(與正則表達式一起):

var doc = XDocument.Load(@"C:\path\to\file.xml"); 
var xpath = "/Adresses/*/XPath"; 
var query = doc.XPathSelectElements(xpath); 
foreach (var element in query) 
{ 
    var newValue = Regex.Replace(element.Value, @"Local\[\d{3}\]", "Local[001]"); 
    element.Value = newValue; 
} 

使用XSLT可能是一種選擇,但我不知道如何在這種情況下,就可以直接使用。但我敢打賭它不會很漂亮(與上面的代碼相比)。

+0

的XML是 //地址/本地[001]/AddressLine1 //地址/本地[002]/AddressLine2 //地址/本地[003]/AddressLine3 //地址/本地[004]/AddressLine4// Address/Local [005]/AddressLine5。我正嘗試將Local [002],[Local003],[Local004]和[Local005]重新映射到Local [001]。 @ Jeff Mercado – ncoder

+0

我做到了這一點:if(xpath.Contains(「// Address」)) {const string LOCAL =「// Address/XPath」; XmlDocument lDoc = new XmlDocument();如果(!dr.IsNull(「document」)) {lDoc.LoadXml(dr [「document」]。ToString()); XmlNodeList localNodes = xDoc.SelectNodes(LOCAL);如果(localNodes!= null) xpath = xpath.Replace(@「Local [\ d {3}]」,「Local [001]」); } } } – ncoder

+0

你究竟想告訴我什麼?我在這裏顯示的代碼應該這樣做... –