LINQ to read XML
Below is my *.xml file having two sections:
<sections>
<section guid="112ff6b8-2609-4d19-b774-33ab951ee66a" last_change="1970-01-01T00:00:00.000" action="added"
name="Concrete sections, Rectangle, 150x300" type="custom" fd-mat="3" fd_name_code="Concrete sections"
fd_name_type="Rectangle" fd_name_size="150x300">
<region_group>
<region>
<contour>
<edge type="line">
<point x="-0.075" y="-0.15" z="0"></point>
<point x="0.075" y="-0.15" z="0"></point>
<normal x="0" y="1" z="0"></normal>
</edge>
<edge type="line">
<point x="0.075" y="-0.15" z="0"></point>
<point x="0.075" y="0.15" z="0"></point>
<normal x="-1" y="0" z="0"></normal>
</edge>
<edge type="line">
<point x="0.075" y="0.15" z="0"></point>
<point x="-0.075" y="0.15" z="0"></point>
<normal x="0" y="-1" z="0"></normal>
</edge>
<edge type="line">
<point x="-0.075" y="0.15" z="0"></point>
<point x="-0.075" y="-0.15" z="0"></point>
<normal x="1" y="0" z="0"></normal>
</edge>
</contour>
</region>
</region_group>
<end></end>
</section>
<section guid="98948ace-afb2-400d-9d96-752f5fce40c4" last_change="1970-01-01T00:00:00.000" action="added"
name="Concrete sections, Rectangle, 300x600" type="custom" fd-mat="3" fd_name_code="Concrete sections"
fd_name_type="Rectangle" fd_name_size="300x600">
<region_group>
<region>
<contour>
<edge type="line">
<point x="-0.15" y="-0.3" z="0"></point>
<point x="0.15" y="-0.3" z="0"></point>
<normal x="0" y="1" z="0"></normal>
</edge>
<edge type="line">
<point x="0.15" y="-0.3" z="0"></point>
<point x="0.15" y="0.3" z="0"></point>
<normal x="-1" y="0" z="0"></normal>
</edge>
<edge type="line">
<point x="0.15" y="0.3" z="0"></point>
<point x="-0.15" y="0.3" z="0"></point>
<normal x="0" y="-1" z="0"></normal>
</edge>
<edge type="line">
<point x="-0.15" y="0.3" z="0"></point>
<point x="-0.15" y="-0.3" z="0"></point>
<normal x="1" y="0" z="0"></normal>
</edge>
</contour>
</region>
</region_group>
<end></end>
</section>
As the results I wish to select (guid, fd_name_size) from the above two sections, as shown below
112ff6b8-2609-4d19-b774-33ab951ee66a, 150x300
98948ace-afb2-400d-9d96-752f5fce40c4, 300x600
Here is my C# code
string a1 = "will be guid"; string a2 = "will be fd_name_size"; // to test
var secBC = from lv1 in xDoc.Descendants("section")
select new
{
Header = lv1.Attribute("guid").Value
// How to select "fd_name_size" ????
};
foreach (var lv1 in secBC)
{
a1 = lv1.Header;
}
I get only the 'guid' (as a1) but do not know how also to select 'fd_name_size'. Please help :o)
lv1.Attribute("fd_name_size").Value
这应该够了吧。
You can do similarly you did it for "guid".
var secBC = from lv1 in xdoc.Descendants("section")
select new
{
Header = lv1.Attribute("guid").Value,
NameSize= lv1.Attribute("fd_name_size").Value
};
foreach (var lv1 in secBC)
{
a1 = lv1.Header;
a2 = lv1.NameSize;
}
Thank you very much :o) The next block of *.xml data “complex_section” include also “section 'guid'”, but not 'fd_name_size', therefore NullReferenceException occurred. The *.xml above is just a part of a long *.xml file. The C #codes below give me the correct values. I love LinQ to XML. Thanks :o)
List<string> guID = new List<string>();
List<string> name = new List<string>();
var secBC = from lv1 in xDoc.Descendants("section")
.Where(lv1 => (string)lv1.Attribute("fd_name_size") != null)
select new
{
Header = lv1.Attribute("guid").Value,
NameSize = lv1.Attribute("fd_name_size").Value
};
foreach (var lv1 in secBC)
{
guID.Add(lv1.Header); // [0] 112ff6b8-2609-4d19-b774-33ab951ee66a, OK
// [1] 98948ace-afb2-400d-9d96-752f5fce40c4, OK
name.Add(lv1.NameSize); // [0] 150x300, OK
// [1] 300x600, OK
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.