質問

I have the following xml File .. I got the content of it after converting a recordset to xml . Now i want some specific value out of it . I am not able to find a way to do that .

I am new to c# . Please help me on it ?

<xml xmlns:s='uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882'
    xmlns:dt='uuid:C2F41010-65B3-11d1-A29F-00AA00C14882'
    xmlns:rs='urn:schemas-microsoft-com:rowset'
    xmlns:z='#RowsetSchema'>
<s:Schema id='RowsetSchema'>
    <s:ElementType name='row' content='eltOnly' rs:updatable='true'>
        <s:AttributeType name='SerialNo' rs:number='1' rs:write='true'>
            <s:datatype dt:type='int' dt:maxLength='4' rs:precision='0'
             rs:fixedlength='true' rs:maybenull='false'/>
        </s:AttributeType>
        <s:AttributeType name='c1' rs:name='Response Type'
             rs:number='2' rs:write='true'>
            <s:datatype dt:type='string' rs:dbtype='str' dt:maxLength='255'
             rs:precision='0' rs:maybenull='false'/>
        </s:AttributeType>
        <s:extends type='rs:rowbase'/>
    </s:ElementType>
</s:Schema>
<rs:data>
    <rs:insert>
        <z:row SerialNo='1' c1='1'/>
        <z:row SerialNo='2' c1='14'/>
        <z:row SerialNo='3' c1='14'/>
        <z:row SerialNo='4' c1='3'/>
        <z:row SerialNo='5' c1='9'/>
        <z:row SerialNo='6' c1='7'/>
        <z:row SerialNo='7' c1='6'/>
        <z:row SerialNo='8' c1='0'/>
        <z:row SerialNo='9' c1='0'/>
    </rs:insert>
</rs:data>
</xml>

In the above xml i want to traverse the below tag and want the value mentioned in serial no and c1 .

         <rs:insert>
            <z:row SerialNo='1' c1='1'/>
            <z:row SerialNo='2' c1='14'/>
            <z:row SerialNo='3' c1='14'/>
            <z:row SerialNo='4' c1='3'/>
            <z:row SerialNo='5' c1='9'/>
            <z:row SerialNo='6' c1='7'/>
            <z:row SerialNo='7' c1='6'/>
            <z:row SerialNo='8' c1='0'/>
            <z:row SerialNo='9' c1='0'/>
        </rs:insert>

please help .. Thanks in advance .

役に立ちましたか?

解決

using Linq To Xml

XNamespace ns = "#RowsetSchema";

var serialNos = XDocument.Load(fileName)
                .Descendants(ns + "row")
                .ToDictionary(r => r.Attribute("SerialNo").Value, 
                              r => r.Attribute("c1").Value);

他のヒント

Maybe something like this:

var MyC1Nodes = new List<string>();
var MySerialNodes = new List<string>();
var MyXml = new XmlDocument();
MyXml.LoadXml(File.ReadAllText(@"pathOfXml").ToString());
foreach (XmlNode tempSerialNo in MyXml.DocumentElement.SelectNodes("//@SerialNo"))
{
    MySerialNodes.Add(tempSerialNo.Value.ToString());
}

foreach (XmlNode tempC1 in MyXml.DocumentElement.SelectNodes("//@c1"))
{
     MyC1Nodes.Add(TempC1.Value.ToString());
}

Edit: IV4answer would be much better for using a dictionary, but also the solution wihout linq:

    var MyData = new Dictionary<int, int>();
    MyXml.LoadXml(File.ReadAllText(@"pathOfXml").ToString());
    var MySerialNodes =  MyXml.DocumentElement.SelectNodes("//@SerialNo");
    var MyC1Nodes =  MyXml.DocumentElement.SelectNodes("//@C1");
    If (MyC1Nodes.Count == MySerialNodes.Count)
    {

    For(int i = 0; i <MySerialNodes.Count; i++)
    {
    MyData.Add((int)MySerialNodes[i],(int) MyC1Nodes[i]); 
    }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top