2013-05-08 64 views
0

這是我的Xml。基於C#中Parent xml屬性的ChildNode

<SCat> 
    <S SId="1" SName="M" FName="MA"> 
    <Cat> 
     <C CId="2" CName="CAS" FName="c-a" /> 
     <C CId="3" CName="DAC" FName="d-a" /> 
    </Cat> 
    </S> 
    <S SId="2" SName="I" FName="IA"> 
    <Cat> 
     <C CId="2" CName="CAS" FName="c-a" /> 
     <C CId="3" CName="DAC" FName="d-a" /> 
    </Cat> 
    </S> 
    <S SId="3" SName="D" FName="DA"> 
    <Cat> 
     <C CId="2" CName="CAS" FName="c-a" /> 
     <C CId="3" CName="DAC" FName="d-a" /> 
    </Cat> 
    </S> 
</SCat> 

我寫了這段代碼。

int Scode = 1; 
dsS = new DataSet(); 
dsS.ReadXml(HttpContext.Current.Server.MapPath(Path)); 

這是我被困住的地方。 我想在DataTable中所有的「貓」,它具有屬性「SID」 = 1

感謝

回答

0

使用下面XPATH,你會得到所有的子節點,其中SId ='1'

/SCat/S[@SId='1']/Cat 
0

您應該使用XmlDocument對象來加載整個文檔,然後提供節點路徑和屬性選擇器。這在MSDN(link1)中都有詳細記錄。以下是該網站的代碼片段:

清單1.

using System; 
using System.IO; 
using System.Xml; 

public class Sample { 

    public static void Main() { 

    XmlDocument doc = new XmlDocument(); 
    doc.LoadXml("<book xmlns:bk='urn:samples' bk:ISBN='1-861001-57-5'>" + 
       "<title>Pride And Prejudice</title>" + 
       "</book>"); 

    XmlNode root = doc.FirstChild; 

    //Create a new attribute. 
    string ns = root.GetNamespaceOfPrefix("bk"); 
    XmlNode attr = doc.CreateNode(XmlNodeType.Attribute, "genre", ns); 
    attr.Value = "novel"; 

    //Add the attribute to the document. 
    root.Attributes.SetNamedItem(attr); 

    Console.WriteLine("Display the modified XML..."); 
    doc.Save(Console.Out); 

    } 
} 

清單2(link2

XElement root = XElement.Load("PurchaseOrder.xml"); 
IEnumerable<XElement> address = 
    from el in root.Elements("Address") 
    where (string)el.Attribute("Type") == "Billing" 
    select el; 
foreach (XElement el in address) 
    Console.WriteLine(el); 

希望這會有所幫助。問候,AB