-3

I am using XDocument.Parse method to load following XML :

<AuditMessage>

    <Event Action="Read" DateTime="2013/26/7" EventID="100"/>
    <User Role="Admin" UserID="12123"/User>
    <SourceIdentification SourceID="TeamLondon" SourceType="3"/>
    <Network AccessPointID="143.176.8.32" AccessPointTypeCode="1" />
    <Network AccessPointID="143.176.8.32" AccessPointTypeCode="`2" />
    <Participant ParticipantID="0001" ParticipantType ="2"/>
    <Participant ParticipantID="0002" ParticipantType ="3"/>
    <Participant ParticipantID="0003" ParticipantType ="3" ParticipantName = "Housh Mangrove"/>

</AuditMessage>

I need to retrieve the values of following attributes in the above XML.

-DateTime    
-Role    
-AccessPointID    
-ParticipantID    
-ParticipantName

I have used sourceXML.Root.Element(nodeName).Attribute(attributeToMatch).Value to read a single attribute. I am failing to understand how can I iterate the same thing over different nodes, provided some nodes might be missing. Please notice :

  1. <Network> and <Participant> nodes are repeating.
  2. ParticipantName attribute exists only in one Instance of
  3. Lastly, any node could be missing in different XMLs provided as Input. Therefore I need to write code in such a way that if a node is missing, the application doesn't throw OBJECT REFERENCE NOT FOUND Exception
4

3 回答 3

0

快速而肮脏的解决方案 - 希望你可以从这里得到它:

var participantID = String.Join(", ",
                                xdoc.Root.Elements("Participant")
                                         .Select(e => e.Attribute("ParticipantID"))
                                         .Where(a => a != null)
                                         .Select(a => a.Value)
                                         .Distinct());
于 2013-10-24T13:36:43.103 回答
0

这是一个快速尝试,您可以找出我没有添加的那些:

public static void Main()
{
    GetAtts(xml);
}

public static Atts GetAtts(string xml)
{
    Atts atts = new Atts();
    XDocument doc = XDocument.Parse(xml);

    if (doc.Root.Element("Event") != null)
        atts.datetime = doc.Root.Element("Event").Attribute("DateTime").Value;

    //...

    foreach (XElement ele in doc.Descendants("Network"))
        atts.accesspointid.Add(ele.Attribute("AccessPointID").Value);


    return atts;
}

public class Atts
{
    public string datetime { get; set; }
    public string role { get; set; }

    private List<string>_accesspointid = new List<string>();
    public List<string> accesspointid { get { return _accesspointid; } set { _accesspointid = value; } }

    public List<string> _participantid = new List<string>();
    public List<string> participantid { get { return _participantid; } set { _participantid = value; } }

    public string participantname { get; set; }
}

您将拥有一个可以更轻松地处理的对象

于 2013-10-24T13:21:36.673 回答
0

您可以使用Elements方法获取给定名称的节点的枚举。

然后,您可以测试枚举是否返回任何结果并在其中查找适当的属性。

像这样的东西,如果你想要它作为 CSV:

var data = new List<string>();

var events = doc.Root.Elements("Event");

if (events.Any())
{
    foreach (var evt in events)
    {
        data.Add(evt.Attribute("DateTime").Value);
    }
}

var participants = doc.Root.Elements("Participant");

if (participants.Any())
{
    foreach (var participant in participants)
    {
        data.Add(participant.Attribute("ParticipantID").Value);
    }
}

var csv = string.Join(", ", data);
于 2013-10-24T13:21:38.213 回答