2012-12-19 4 views
0

다음 예제가 있고 C#은 그냥 초안입니다. 당신이 어떻게 가치를 얻을 수 있도록 XML 파일을 호출하고이 읽는 저를 보여줄 수xml C# 읽기 및 쓰기 도우미

public static ArrayList GetLocationLiabilityAmount() 
{ 
    ArrayList al = new ArrayList(); 
    string selectedValue = Library.MovieClass.generalLibailityLocationLiability; 
    if (!String.IsNullOrEmpty(selectedValue)) 
    { 
     if (option from xml == selectedValue) 
     { 
      al.Add(minvalue); 
      al.Add(maxvalue); 
     } 
     return al; 
    } 
    else 
    { 
     return null; 
    } 
} 

XML :

<?xml version="1.0" encoding="utf-8" ?> 
<AccidentMedicalCoverage> 
    <coverage option="1" value="10000" showvalue="$10,000 per person"></coverage> 
    <coverage option="2" value="25000" showvalue="$25,000 per person"></coverage> 
    <coverage option="3" value="50000" showvalue="$50,000 per person"></coverage> 
</AccidentMedicalCoverage> 
+0

있다 <1 인당 $ 25,000> showvalue = "1 인당 25,000 달러"> c : \\ xmlfile \ coverage.xml – user1883676

+0

무엇이'minvalue'와'maxvalue'입니까? XML에서 단 하나의 'value'속성 만 볼 수 있습니다. 왜 강력하게 타입이 지정된리스트 대신에'ArrayList'를 사용하고 있습니까? –

답변

1

질문입니다 너무 명확하지만 내가 원하는 가정 것입니다 :

주어진 option 당신이 XML에서 value를 얻으려면이 하나의 방법 당신은 그것을 할 수있다 :

XmlDocument xDoc = new XmlDocument(); 
xDoc.Load("c:\\xmlfile\\coverage.xml"); 

// Select the node with option=1 
XmlNode node = xDoc.SelectSingleNode("/AccidentMedicalCoverage/coverage[@option='1']"); 
// Read the value of the Attribute 'value' 
var value = node.Attributes["value"].Value; 
1

나는 linq to XML을 선호합니다. 두 개의 아래 나타낸하여 XDocument에 데이터를 얻기 위해 특정 방식으로 상기 데이터에 대한 기본적인 질의

//var xml = File.ReadAllText(@"C:\data.xml"); 
    var xml = GetFile(); 

    //var xDoc = XDocument.Load(@"C:\data.xml"); Alternate 
    var xDoc = XDocument.Parse(xml); 

    var coverages = xDoc.Descendants("coverage"); 

    coverages.Select (cv => cv.Attribute("showvalue").Value) 
      .ToList() 
      .ForEach(showValue => Console.WriteLine (showValue)); 

/* Output 
$10,000 per person 
$25,000 per person 
$50,000 per person 
*/ 

... 

public string GetFile() 
{ 
return @"<?xml version=""1.0"" encoding=""utf-8"" ?> 
<AccidentMedicalCoverage> 
    <coverage option=""1"" value=""10000"" showvalue=""$10,000 per person""></coverage> 
    <coverage option=""2"" value=""25000"" showvalue=""$25,000 per person""></coverage> 
    <coverage option=""3"" value=""50000"" showvalue=""$50,000 per person""></coverage> 
</AccidentMedicalCoverage>"; 
}